Session 4 |
String class methods
We had our first view of strings in the first session (see here): a string is any sequence of characters – letters, digits, spaces, punctuation, and other “special characters” placed between the quotes.
In this section we going to have a closer look at the methods applicable to strings:
Useful link: the full list of String class fields, constructors, methods and their description can be found here. Pay attention to signatures (the number of parameters and their data type) and the return type!
To create a String instance, we can use one of the String constructors whose argument is an array of chars:
char[] arrayOfChars = {'G','o','o','d',' ','m','o','r','n','i','n','g'}; String message = new String(arrayOfChars); System.out.println(message);
An instance of String class can be created in a simpler way:
String message = "Good morning";
Once we have an instance, we can use String class methods:
public class HelloString { public static void main(String[] args) { String myString = "Mart Mardikas"; System.out.println("String length: " + myString.length()); // 13 System.out.println(myString.startsWith("Mart")); // true System.out.println(myString.endsWith("kas")); // true System.out.println(myString.endsWith("Mart")); // false System.out.println("Index of the first 'a': " + myString.indexOf('a')); // 1 int rIndex = myString.indexOf('r'); System.out.println("Index of the first 'r': " + rIndex); // 2 System.out.println("Index of the next 'r': " + myString.indexOf('r', rIndex + 1)); // 7 int aIndex = myString.lastIndexOf('a'); System.out.println("Index of the last 'a': " + aIndex); // 11 System.out.println("Index of the Substring \\"Mardi\\": " + myString.indexOf("Mardi")); // 5 System.out.println("The 4th letter: "+myString.charAt(3)); // 't' //Compare two strings System.out.println(myString.equals("Mart Mardikas")); // true System.out.println(myString.equals("mart mardikas")); // false //Compare two strings ignoring case sensitivity System.out.println(myString.equalsIgnoreCase("mart mardikas")); // true //Lexicographical comparison System.out.println(myString.compareTo("Jaan Jaaniste")); // >0 System.out.println(myString.compareTo("Peeter Paan")); // <0 System.out.println(myString.compareTo("Mart Mardikas")); // =0 System.out.println(myString.replace('M', 'P')); // "Part Pardikas" System.out.println(myString.toUpperCase()); // "MART MARDIKAS" //String with spaces and new lines removed at the beginning and end of the string String yourString = " Mart \n"; System.out.println(yourString.trim()); // "Mart" //Split the string near "ar" String[] stringParts = myString.split("ar"); for (int i = 0; i<stringParts.length; i++){ System.out.println(stringParts[i]); // { "M", "t M", "dikas" } } } }
Pay attention that the relational operator ==
compares if two strings refer to the same object. If we want to compare the content of two strings, we have to use special methods like equals
, equalsIgnoreCase
, compareTo
, startsWith
, endsWith
etc.
Try out
Compile and run the code above.
Update the program so that it would also print "Harry and the 'chamber' of secrets"
(including the single and double quotes).
Session 4 |