1 / 32

Strings in Java

Strings in Java. What data types have we seen so far?. Data types we’ve seen so far…. integer types: int (also short, char, and byte) real numbers: double and float. Introducing the String. S tring Case sensitive (capital S is required ) Declaring a String

nmatson
Download Presentation

Strings in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings in Java

  2. What data types have we seen so far?

  3. Data types we’ve seen so far… • integer types: • int • (also short, char, and byte) • real numbers: • double and float

  4. Introducing the String • String • Case sensitive (capital S is required) • Declaring a String • How did we declare variables of the other types (such as int, double, float) in the past?

  5. Declarations • How have we already declared variables? inti; float f; double x; • So how do we declare Strings?

  6. Declaring Strings //declare variables int beta; double delta; String sigma;

  7. Assigning values to Strings //declare variables int beta; double delta; String sigma; //initialize variables beta = 1; delta = 2.0; sigma = "hello there";

  8. Declaration w/ assignment //declare & init variables int beta = 1; double delta = 2.0; String sigma = "hello there";

  9. String is a Java class • Instances of class String are objects. • We can print strings. String s = "hello"; System.out.println( s );

  10. String is a Java class • Instances of class String are objects. • Useful String method: equality String s = "hello"; System.out.println( s.equals("Hello") );

  11. String is a Java class (like input and output) • Useful String method: equality String s = "hello"; System.out.println( "s is equal to hello is " + s.equals("Hello") ); s is equal to hello is false (boolean)

  12. String methods • equals() is case sensitive (i.e., it distinguishes between upper and lower case) • "hello" is not the same as "Hello" • (Note: Don’t use ==.)

  13. String methods • Wouldn’t it be nice to have a method that is like equals() but ignores case? • "hello" is the same as "Hello" • What would be a good name for such a method?

  14. String s = "HeLlo"; System.out.println( "s is equal to hello is " + s.equalsIgnoreCase("HELLO") );

  15. String s = "HeLlo"; System.out.println( "s is equal to hello is " + s.equalsIgnoreCase("HELLO") ); s is equal to hello is true

  16. String methods so far • So far: • equals() • equalsIgnoreCase()

  17. More String methods • length() • startsWith() • endsWith() • toLowerCase() • toUpperCase() • charAt() • substring()

  18. String • Who defined the String class? • Is there documentation for the String class?

  19. String • Who defined the String class? • The Sun Microsystems company when it defined Java. • Is there documentation for the String class? How many methods are there? • Lots! (over 75!)

  20. The award for the longest name for a person belongs to a German immigrant to Philadelphia, Pennsylvania. The name he was given at birth, and which somehow fit on his passport was: (First and "middle" names) Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvim John Kenneth Loyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor Willian Xerxes Yancy Zeus (Last name) Wolfeschlegelsteinhausenbergerdorffvoralternwarengewissenhaf tschaferswesenchafewarenwholgepflegeundsorgfaltigkeitbeschut zenvonangereifenduchihrraubgiriigfeindewelchevorralternzwolf tausendjahresvorandieerscheinenbanderersteerdeemmeshedrraums chiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefah rthinzwischensternartigraumaufdersuchenachdiesternwelshegeha btbewohnbarplanetenkreisedrehensichundwohinderneurassevanver standigmenshlichkeittkonntevortpflanzenundsicherfreunanleben slamdlichfreudeundruhemitnichteinfurchtvorangreifenvonandere rintlligentgeschopfsvonhinzwischensternartigraum Senior

  21. String method: length() String nm = "john jacobjingleheimerschmidt"; System.out.print( "Wow. " ); System.out.println( "You name is " + nm.length() + " long." ); What (type of thing) does length() return?

  22. String method: length() //define first name String fn = "george"; //define last name String ln = "grevera"; inttotalLength = fn.length() + ln.length();

  23. More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1).

  24. More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1). String str = "fred loves ethel"; System.out.println( str.charAt(1) );

  25. More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1). String str = "fred loves ethel"; System.out.println( str.charAt(1) ); r

  26. More string methods substring() (Two of them.) • String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). • String substring ( int beginIndex, int endIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1 starting with 0).

  27. More string methods String substring ( intbeginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). String p = "fred loves ethel"; System.out.println( p.substring(1) );

  28. More string methods String substring ( intbeginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). String p = "fred loves ethel"; System.out.println( p.substring(1) ); red loves ethel

  29. More string methods String substring ( intbeginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). System.out.println( "fred loves ethel".substring(1) );

  30. More string methods String substring ( intbeginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). System.out.println( "fred loves ethel".substring(1) ); red loves ethel

  31. More string methods String substring ( intbeginIndex, intendIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1 starting with 0). System.out.println( "fred loves ethel".substring(1,4) + "." );

  32. More string methods String substring ( intbeginIndex, intendIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1 starting with 0). System.out.println( "fred loves ethel".substring(1,4) + "." ); red.

More Related