String is use to store text such as a word, a sentence, a story, a file, or anything that is not of integer.
String is a collection or sequence of characters. In java we use double quotes instead of single quotes to create string.
There are two ways to create a String:
1. String literal
2. new keyword
String name = "Folau"; // don't do this, '' or single quotes are for chars String firstName = 'Folau';
String in immutable which means once you have created a string you could no longer change its value. However, a variable as a String reference can be changed to point to some other string value. Remember that everytime you change or edit a string you are creating a new string value.
String name = "Folau"; name = "Lisa"; // In memory we did not overide "Folau" to "Lisa". We created // a new String "Lisa" and point name to it. Now we have two strings // in memory "Folau" and "Lisa"
String name = "Folau"; String firstName = "Folau"; // if we want to use a differenct object in memory then we must use the new keyword to create our string.
Creating String with the new keyword
String name = new String(“Folau”);
To find out the length of a String, use the length() method.
String name = "Folau"; int nameLength = name.length(); System.out.println("The length your name: " + nameLength);// 5
To find out if a string contains another string, use indexOf(). IndexOf() returns the position of the first occurrence of the searching string.
String name = "Folau"; String searchingString = "lau"; int position = name.indexOf("lau"); System.out.println("position: " + position);// 2
To add or put two Strings together, there are two ways to do it. First you can use the + operator
String firstName = "Folau"; String lastName = "Kaveinga"; String name = firstName + " " + lastName; System.out.println("name: " + name);// Folau Kaveinga
and second we use the concat() method
String firstName = "Folau "; String lastName = "Kaveinga"; String name = firstName.concat(lastName); System.out.println("name: " + name);// Folau Kaveinga
Using of + operator with String
adding two strings together will concatenate.
String a = "Folau "; String b = "Kaveinga"; String c = a + b; System.out.println("c: " + c);// Folau Kaveinga
Using of + operator with integer
adding two numbers together will add or sum.
int a = 5; int b = 10; int c = a + b; System.out.println("c: " + c);// 15
replace(char oldChar, char newChar) – it replaces the oldChar with the newChar.
String sentence = "My name is Folau"; String newSentence = sentence.replace("a","o"); System.out.println("newSentence: " + newSentence);// My nome is Folou
toLowerCase converts all characters within a string to lowercase.
String name = "FOLAU"; String lowercaseName = name.toLowerCase(); System.out.println("lowercaseName: " + lowercaseName);// folau
toUpperCase coverts all characters within a string to uppercase.
String name = "folau"; String uppercaseName = name.toUpperCase(); System.out.println("uppercaseName: " + uppercaseName);// FOLAU
String join(String delimeter, String … elements)
join() joins strings of elements together with delimeter
String sentence = String.join("^","I","have","to","do","this"); System.out.println("sentence: " + sentence);// I^have^to^do^this
To extract a single character from a String, you can refer directly to an individual character via the charAt( ) method.
char charAt(int where)
where is the index of the character that you want to obtain. The value of where must be nonnegative and specify a location within the string.
charAt( ) returns the character at the specified location.
If you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray( ). It returns an array of characters for the entire string.
char[ ] toCharArray( )
String equals vs ==
It is important to understand that the equals( ) method and the == operator perform two different operations. The equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance.
Often, it is not enough to simply know whether two strings are identical. For sorting applications, you need to know which is less than, equal to, or greater than the next. A string is less than another if it comes before the other in dictionary order. A string is greater than another if it comes after the other in dictionary order. The method compareTo( ) serves this purpose. It is specified by the Comparable<T> interface, which String implements.
int compareTo(String str)
The result of the comparison is returned and is interpreted as shown here.
If you want to ignore case differences when comparing two strings, use compareToIgnoreCase( )
String indexOf( )
Searches for the first occurrence of a character or substring.
String lastIndexOf( )
Searches for the last occurrence of a character or substring.