1 / 11

Strings

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 )

Download Presentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.

  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. • “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 • “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 • “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 • “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

More Related