Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings Methods in the String class Manipulating text in Java.

Similar presentations


Presentation on theme: "Strings Methods in the String class Manipulating text in Java."— Presentation transcript:

1 Strings Methods in the String class Manipulating text in Java

2 The String Class ► Like all standard java classes, there is a javadoc for the String class that can be helpful to you. ► The methods our class is responsible for are:  length()  indexOf( String )  indexOf( String, int )  substring( int, int )  substring( int )

3 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 Index012345678

4 Length ► Strings can tell you how many characters they have by using the length method:  Ex. ► String name1 = new String( “Sally” ); ► String name2 = new String( “John” ); ► String name3 = new String( “Bob” ); ► String name4 = new String( “Mary Sue” ); ► System.out.println( name1.length() );//prints 5 ► System.out.println( name2.length() );//prints 4 ► System.out.println( name3.length() );//prints 3 ► System.out.println( name4.length() );//prints 8  White space counts when asking for length!

5 Substrings ► Strings have a method called substring that can give you pieces of the string ► substring takes the starting index and one past the stopping index ► Ex. CharacterMicrosoft Index012345678  “Microsoft”.substring( 0, 5 );  returns “Micro”  “Microsoft”.substring( 5, 9 );  returns “Soft”  “Microsoft”.substring( 3, 7 );  returns “roso”

6 Substring ► substring has another form: ► if you give substring the starting index only, it goes to the end of the String  “Microsoft”.substring( 3 ); //returns “rosoft”  “Microsoft”.substring( 5 ); //returns “soft”  “Microsoft”.substring( 7 ); //returns “ft”

7 indexOf ► The indexOf method returns the index where a substring is found CharacterMicrosoft Index012345678  “Microsoft”.indexOf( “Micro” );  returns 0  “Microsoft”.indexOf( “soft” );  returns 5  “Microsoft”.indexOf( “icro” );  returns 1

8 indexOf ► When a character is not found indexOf returns -1, an invalid index, to let the caller know ► When a character or substring can be found more than once, indexOf returns the first occurrence CharacterMicrosoft Index012345678  “Microsoft”.indexOf( “m” );  returns -1  “Microsoft”.indexOf( “apple” );  Returns -1  “Microsoft”.indexOf( “o” );  returns 4

9 indexOf ► indexOf has another parameter ► You can tell it where to start looking CharacterMicrosoft Index012345678  “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

10 The Last Index ► Finding the last index in a String means you’ll need to use the length() method ► Remember, Strings are 0-based! ► The last index is length() – 1 ► Ex.  String name = new String( “Billy” ); String lastChar = new String(); int lastIndex = name.length() – 1; lastChar = name.substring( lastIndex ); System.out.println( lastChar );//prints ‘y’

11 Other String Methods ► I encourage you to refer to the JavaDoc online for Strings, there are many methods that you may find helpful as we create new projects. ► Later on I will review with you the methods:  equals  compareTo ► Some suggestions that can be helpful:  replace  trim  valueOf  toUpperCase  toLowerCase  matches ► You’ll also need to learn about a very useful programming concept called regular expressions in order to use this method


Download ppt "Strings Methods in the String class Manipulating text in Java."

Similar presentations


Ads by Google