Download presentation
Presentation is loading. Please wait.
Published byἸωήλ Δαμασκηνός Modified over 5 years ago
1
Methods in the String class Manipulating text in Java
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. Character M i c r o s f t Index 1 2 3 4 5 6 7 8
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. Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “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 Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “Microsoft”.indexOf( “Micro” ); returns 0 “Microsoft”.indexOf( “soft” ); returns 5 “Microsoft”.indexOf( “icro” ); returns 1
8
indexOf Character M i c r o s f t Index 1 2 3 4 5 6 7 8
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 Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “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 Character M i c r o s f t Index 1 2 3 4 5 6 7 8 “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. We’ve discussed these for comparison with keys in Greenfoot: 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
12
Example
13
Example Write a method to check if a password meets the following criteria Contains a number Contains an uppercase letter Is at least 9 letters long
14
Example public boolean passwordOK( String pwd ) { boolean hasUppercase = false; for( int n = 0; n < pwd.length() - 1; n++ ) String ch = pwd.substring(n, n+1); if(ch.compareTo(“A”) >= 0 && ch.compareTo(“Z”) <= 0) hasUppercase = true; } boolean hasDigit = false; if(ch.compareTo(“0”) >= 0 && ch.compareTo(“9”) <= 0) hasDigit = true; return pwd.length() >= 9 && hasDigit && hasUppercase;
15
Example Write a method to count the number of times the word “Java” appears in a string
16
Example public int count(String words) { String target = “Java”; int foundAt = words.indexOf( target ); int count = 0; while( foundAt >= 0 ) count++; foundAt = words.indexOf( target, foundAt + target.length()); } return count;
17
Example Let’s say * is a wildcard and we want to know if a String contains a particular substring public int countPattern( String source, String target ) countPattern( “Hello World”, “orl” ) 1 countPattern( “He**o World”, “Help”) 1 countPattern( “H**lo World”, “He” ) 2 (once for He, and **)
18
Example public int countPattern(String source, String target) { int count = 0; for(int n = 0; n < source.length() - 1; n++) int matches = 0; for(int k = 0; k < target.length() – 1 && n + k < source.length() - 1; k++) String sourceChar = source.substring(n + k, n + k +1); String targetChar = target.substring(k, k+1); if(sourceChar.compareTo(“*”) == 0)//wildcard matches++; else if( sourceChar.compareTo( targetChar ) == 0 ) else k = target.length();//stop the loop can also use break but not as elegant } if(matches == target.length()) count++; return count;
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.