110 likes | 246 Views
Strings. Methods in the String class Manipulating text in Java. 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 )
E N D
Strings Methods in the String class Manipulating text in Java
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 )
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.
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!
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. • “Microsoft”.substring( 0, 5 ); • returns “Micro” • “Microsoft”.substring( 5, 9 ); • returns “Soft” • “Microsoft”.substring( 3, 7 ); • returns “roso”
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”
indexOf • The indexOf method returns the index where a substring is found • “Microsoft”.indexOf( “Micro” ); • returns 0 • “Microsoft”.indexOf( “soft” ); • returns 5 • “Microsoft”.indexOf( “icro” ); • returns 1
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 • “Microsoft”.indexOf( “m” ); • returns -1 • “Microsoft”.indexOf( “apple” ); • Returns -1 • “Microsoft”.indexOf( “o” ); • returns 4
indexOf • indexOf has another parameter • You can tell it where to start looking • “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
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’
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