Strings in Java
What data types have we seen so far?
Data types we’ve seen so far… integer types: integer types: –int –(also short, char, and byte) real numbers: real numbers: –double and float
Introducing the String String String –Case sensitive (capital S is required) Declaring a String Declaring a String –How did we declare variables of the other types (such as int, double, float) in the past?
Declarations How have we already declared variables? How have we already declared variables? inti; floatf; doublex; So how do we declare Strings? So how do we declare Strings?
Declaring Strings //declare variables int beta; double delta; String sigma;
Assigning values to Strings //declare variables int beta; double delta; String sigma; //initialize variables beta = 1; delta = 2.0; sigma = "hello there";
Declaration w/ assignment //declare & init variables int beta = 1; double delta = 2.0; String sigma = "hello there";
String is a Java class Instances of class String are objects. Instances of class String are objects. We can print strings. We can print strings. String s = "hello"; System.out.println( s );
String is a Java class Instances of class String are objects. Instances of class String are objects. Useful String method: equality Useful String method: equality String s = "hello"; System.out.println( s.equals("Hello") );
String is a Java class (like input and output) Useful String method: equality Useful String method: equality String s = "hello"; System.out.println( "s is equal to hello is " + s.equals("Hello") ); s is equal to hellois false (boolean)
String methods equals() is case sensitive (i.e., it distinguishes between upper and lower case) equals() is case sensitive (i.e., it distinguishes between upper and lower case) –"hello" is not the same as "Hello" –(Note: Don’t use ==.)
String methods Wouldn’t it be nice to have a method that is like equals() but ignores case? 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?
String s = "HeLlo"; System.out.println( "s is equal to hello is " + s.equalsIgnoreCase("HELLO") );
String s = "HeLlo"; System.out.println( "s is equal to hello is " + s.equalsIgnoreCase("HELLO") ); s is equal to hellois true
String methods so far So far: So far: –equals() –equalsIgnoreCase()
More String methods length() length() startsWith() startsWith() endsWith() endsWith() toLowerCase() toLowerCase() toUpperCase() toUpperCase() charAt() charAt() substring() substring()
String Who defined the String class? Who defined the String class? Is there documentation for the String class? Is there documentation for the String class?
String Who defined the String class? 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? Is there documentation for the String class? How many methods are there? –Lots! (over 75!) Lots!
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) WolfeschlegelsteinhausenbergerdorffvoralternwarengewissenhaftschaferswesenchafewarenwholgepflegeundsorgfaltigkeitbeschutzenvonangereifenduchihrraubgiriigfeindewelchevorralternzwolftausendjahresvorandieerscheinenbanderersteerdeemmeshedrraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelshegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevanverstandigmenshlichkeittkonntevortpflanzenundsicherfreunanlebenslamdlichfreudeundruhemitnichteinfurchtvorangreifenvonandererintlligentgeschopfsvonhinzwischensternartigraumSenior
String method: length() String nm = "john jacob jingleheimer schmidt"; System.out.print( "Wow. " ); System.out.println( "You name is " + nm.length() + " long." ); What (type of thing) does length() return?
String method: length() //define first name String fn = "george"; //define last name String ln = "grevera"; int totalLength = fn.length() + ln.length();
More string methods charAt() char charAt ( int index ) Returns the char value at the specified index (0..N-1).
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) );
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
More string methods substring()substring()(Two of them.) substring() 1. String substring ( int beginIndex ) Returns a new string that is a substring of this string (from beginIndex..end starting with 0). 2. String substring ( int beginIndex, int endIndex ) Returns a new string that is a substring of this string (from beginIndex..endIndex-1 starting with 0).
More string methods String substring ( int beginIndex ) 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) );
More string methods String substring ( int beginIndex ) 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
More string methods String substring ( int beginIndex ) 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) );
More string methods String substring ( int beginIndex ) 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
More string methods String substring ( int beginIndex, int endIndex ) 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) + "." );
More string methods String substring ( int beginIndex, int endIndex ) 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.