Download presentation
Presentation is loading. Please wait.
Published byOsborn Glenn Modified over 8 years ago
1
Strings in Java
2
What data types have we seen so far?
3
Data types we’ve seen so far… integer types: integer types: –int –(also short, char, and byte) real numbers: real numbers: –double and float
4
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?
5
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?
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. Instances of class String are objects. We can print strings. We can print strings. String s = "hello"; System.out.println( s );
10
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") );
11
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)
12
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 ==.)
13
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?
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 hellois true
16
String methods so far So far: So far: –equals() –equalsIgnoreCase()
17
More String methods length() length() startsWith() startsWith() endsWith() endsWith() toLowerCase() toLowerCase() toUpperCase() toUpperCase() charAt() charAt() substring() substring()
18
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?
19
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!
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) WolfeschlegelsteinhausenbergerdorffvoralternwarengewissenhaftschaferswesenchafewarenwholgepflegeundsorgfaltigkeitbeschutzenvonangereifenduchihrraubgiriigfeindewelchevorralternzwolftausendjahresvorandieerscheinenbanderersteerdeemmeshedrraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelshegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevanverstandigmenshlichkeittkonntevortpflanzenundsicherfreunanlebenslamdlichfreudeundruhemitnichteinfurchtvorangreifenvonandererintlligentgeschopfsvonhinzwischensternartigraumSenior
21
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?
22
String method: length() //define first name String fn = "george"; //define last name String ln = "grevera"; int totalLength = 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()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).
27
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) );
28
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
29
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) );
30
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
31
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) + "." );
32
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.