11
Strings Strings Methods in the String Methods in the String class class Manipulating text in Java Manipulating text in Java

Strings Methods in the String class Manipulating text in Java

Embed Size (px)

DESCRIPTION

Strings ► A string is a series of characters, also known as an array. ► Arrays are 0-based meaning we start counting at 0 rather than one ► Ex. CharacterMicrosoft Index

Citation preview

Page 1: Strings Methods in the String class Manipulating text in Java

StringsStringsMethods in the String classMethods in the String classManipulating text in JavaManipulating text in Java

Page 2: Strings Methods in the String class Manipulating text in Java

The String ClassThe String Class► Like all standard java classes, there is a Like all standard java classes, there is a

javadoc for the String class that can be javadoc for the String class that can be helpful to you.helpful to you.

► The methods our class is responsible for are:The methods our class is responsible for are: length()length() indexOf( String )indexOf( String ) indexOf( String, int )indexOf( String, int ) substring( int, int )substring( int, int ) substring( int )substring( int )

Page 3: Strings Methods in the String class Manipulating text in Java

StringsStrings► A string is a series of characters, also known as an A string is a series of characters, also known as an

array.array.► Arrays are 0-based meaning we start counting at 0 Arrays are 0-based meaning we start counting at 0

rather than onerather than one► Ex.Ex.

CharacteCharacterr

MM ii cc rr oo ss oo ff tt

IndexIndex 00 11 22 33 44 55 66 77 88

Page 4: Strings Methods in the String class Manipulating text in Java

LengthLength► Strings can tell you how many characters Strings can tell you how many characters

they have by using the length method:they have by using the length method: Ex.Ex.

►String name1 = new String( “Sally” );String name1 = new String( “Sally” );►String name2 = new String( “John” );String name2 = new String( “John” );►String name3 = new String( “Bob” );String name3 = new String( “Bob” );►String name4 = new String( “Mary Sue” );String name4 = new String( “Mary Sue” );►System.out.println( name1.length() );//prints 5System.out.println( name1.length() );//prints 5►System.out.println( name2.length() );//prints 4System.out.println( name2.length() );//prints 4►System.out.println( name3.length() );//prints 3System.out.println( name3.length() );//prints 3►System.out.println( name4.length() );//prints 8System.out.println( name4.length() );//prints 8

White space counts when asking for length!White space counts when asking for length!

Page 5: Strings Methods in the String class Manipulating text in Java

SubstringsSubstrings► Strings have a method called substring that can Strings have a method called substring that can

give you pieces of the stringgive you pieces of the string► substring takes the starting index and one past the substring takes the starting index and one past the

stopping indexstopping index► Ex.Ex.

CharactCharacterer

MM ii cc rr oo ss oo ff tt

IndexIndex 00 11 22 33 44 55 66 77 88““Microsoft”.substring( 0, 5 ); Microsoft”.substring( 0, 5 );

returns “Micro”returns “Micro”““Microsoft”.substring( 5, 9 ); Microsoft”.substring( 5, 9 );

returns “Soft”returns “Soft”““Microsoft”.substring( 3, 7 ); Microsoft”.substring( 3, 7 );

returns “roso”returns “roso”

Page 6: Strings Methods in the String class Manipulating text in Java

SubstringSubstring►substring has another form:substring has another form:► if you give substring the starting index if you give substring the starting index

only, it goes to the end of the Stringonly, it goes to the end of the String ““Microsoft”.substring( 3 ); //returns Microsoft”.substring( 3 ); //returns

“rosoft”“rosoft” ““Microsoft”.substring( 5 ); //returns “soft”Microsoft”.substring( 5 ); //returns “soft” ““Microsoft”.substring( 7 ); //returns “ft”Microsoft”.substring( 7 ); //returns “ft”

Page 7: Strings Methods in the String class Manipulating text in Java

indexOfindexOf► The indexOf method returns the index The indexOf method returns the index

where a substring is foundwhere a substring is found

CharacteCharacterr

MM ii cc rr oo ss oo ff tt

IndexIndex 00 11 22 33 44 55 66 77 88“Microsoft”.indexOf( “Micro” );

returns 0“Microsoft”.indexOf( “soft” );

returns 5“Microsoft”.indexOf( “icro” );

returns 1

Page 8: Strings Methods in the String class Manipulating text in Java

indexOfindexOf► When a character is not found indexOf returns -1, an invalid When a character is not found indexOf returns -1, an invalid

index, to let the caller knowindex, to let the caller know► When a character or substring can be found more than once, When a character or substring can be found more than once,

indexOf returns the first occurrenceindexOf returns the first occurrence

CharacteCharacterr

MM ii cc rr oo ss oo ff tt

IndexIndex 00 11 22 33 44 55 66 77 88“Microsoft”.indexOf( “m” );

returns -1“Microsoft”.indexOf( “apple” );

Returns -1“Microsoft”.indexOf( “o” );

returns 4

Page 9: Strings Methods in the String class Manipulating text in Java

indexOfindexOf► indexOf has another parameterindexOf has another parameter► You can tell it where to start lookingYou can tell it where to start looking

CharacteCharacterr

MM ii cc rr oo ss oo ff tt

IndexIndex 00 11 22 33 44 55 66 77 88“Microsoft”.indexOf( “o”, 5 );

returns 6;//skips past the first ‘o’ by starting at index 5“Microsoft”.indexOf( “s”, 3 );

returns 5;// still finds the first s“Microsoft”.indexOf( “M”, 5 );

returns -1;// no ‘M’ after index 5

Page 10: Strings Methods in the String class Manipulating text in Java

The Last IndexThe Last Index► Finding the last index in a String means Finding the last index in a String means

you’ll need to use the length() methodyou’ll need to use the length() method► Remember, Strings are 0-based!Remember, Strings are 0-based!► The last index is length() – 1 The last index is length() – 1 ► Ex.Ex.

String name = new String( “Billy” );String name = new String( “Billy” );String lastChar = new String();String lastChar = new String();int lastIndex = name.length() – 1;int lastIndex = name.length() – 1;lastChar = name.substring( lastIndex );lastChar = name.substring( lastIndex );System.out.println( lastChar );//prints ‘y’System.out.println( lastChar );//prints ‘y’

Page 11: Strings Methods in the String class Manipulating text in Java

Other String MethodsOther String Methods► I encourage you to refer to the JavaDoc online for I encourage you to refer to the JavaDoc online for

Strings, there are many methods that you may find Strings, there are many methods that you may find helpful as we create new projects.helpful as we create new projects.

► Later on I will review with you the methods:Later on I will review with you the methods: equalsequals compareTocompareTo

► Some suggestions that can be helpful:Some suggestions that can be helpful: replacereplace trimtrim valueOfvalueOf toUpperCasetoUpperCase toLowerCasetoLowerCase matchesmatches

► You’ll also need to learn about a very useful programming You’ll also need to learn about a very useful programming concept called regular expressions in order to use this concept called regular expressions in order to use this methodmethod