Chapter 4 |
Methods of class String
We had our first view of strings in the first chapter (see here): a string is any sequence of characters – letters, digits, spaces, punctuation, and other “special characters” placed between quotes.
In this section, we are going to have a closer look at methods applicable to strings:
Useful link: full list of String class fields, constructors, methods and their description can be found here. Pay attention to the signatures (the number of parameters and their data types) and the return type!
To create a String object, we can use one of the String constructors, the argument of which is an array of chars (each char is written between single quotes!):
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 also be created in a simpler way:
String message = "Good morning";
Once we have an instance of String data type, we can use methods of its class:
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 ==
checks 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 program above.
Modify the program so that it would also print "Harry and the 'chamber' of secrets"
(including the single and double quotes).
Chapter 4 |