320 likes | 354 Views
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
E N D
Data types we’ve seen so far… • integer types: • int • (also short, char, and byte) • real numbers: • double and float
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?
Declarations • How have we already declared variables? inti; float f; double x; • 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. • We can print strings. String s = "hello"; System.out.println( s );
String is a Java class • Instances of class String are objects. • 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 String s = "hello"; System.out.println( "s is equal to hello is " + s.equals("Hello") ); s is equal to hello is false (boolean)
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 ==.)
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?
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 hello is true
String methods so far • So far: • equals() • equalsIgnoreCase()
More String methods • length() • startsWith() • endsWith() • toLowerCase() • toUpperCase() • charAt() • substring()
String • Who defined the String class? • Is there documentation for the String class?
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!)
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
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?
String method: length() //define first name String fn = "george"; //define last name String ln = "grevera"; inttotalLength = 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() (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).
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) );
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
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) );
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
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) + "." );
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.