The String
class represents character strings in UTF-16 encoding. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers ( StringBuffer and StringBuilder ) support mutable strings. Because String objects are immutable they can be shared. After creating string “abc” it will be added to pool of string in the runtime and if another variable has the same value “abc” then this variable just will point to the initial string so there will be only one imitable representation of that string “abc” . This way very big strings can be reused many times without big memory footprint
Example of declaration of string:
String str = "abc";
Here are some more examples of how strings can be used directly without to be declared in variable first:
System.out.println("abc");
String b = "abc".substring(2,3);
The class String
includes many methods, some of which:
- for examining individual characters of the sequence ->
charAt(int index)
- for comparing strings ->
compareTo(String anotherString)
,compareToIgnoreCase(String str)
- for searching strings ->
contains(CharSequence s)
,matches(String regex)
- for extracting substrings ->
subSequence(int beginIndex, int endIndex)
,split(String regex)
- for creating a copy of a string with all characters translated to uppercase or to lowercase ->
toUpperCase()
,toLowerCase()
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings:
System.out.println("abc" + "def");
Inside Object class there is method toString which is used for string conversion. All Java classes have this method but in many cases this method must be overridden to show more sensible information. By default toString implementation of Object will return:
this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
Which in most cases will not help us much. Example of override of Class Person with fields name and age:
class Person {
String name;
Integer age;
@Override
public String toString() {
return "Person{ name=" + name +", age=" + age +"}";
}
}
Example program using some of the string most common methods:
public class MainClassStringTest { public static void main(String[] args) { String a = "This is a simple string"; // find if word simple is inside of string and get first index occurrence int indexOfSimple = a.indexOf("simple"); System.out.println("Index of simple inside string 'a': " + indexOfSimple); System.out.println("--------------------------------------"); // substring from indexOfSimple until the end of the string String simpleString = a.substring(indexOfSimple); System.out.println("simpleString value: " + simpleString); System.out.println("--------------------------------------"); // split the substring by empty spaces String[] words = simpleString.split(" "); for(String word: words) { System.out.println("Word inside simpleString: " + word); } System.out.println("--------------------------------------"); String simpleWord = words[0]; // This should give us value 'simple' if("SIMPLE".equalsIgnoreCase(simpleWord)) { System.out.println("Equal 'SIMPLE' and '" + simpleWord + "' with equalsIgnoreCase"); } System.out.println("--------------------------------------"); // Get byte array and print the byres byte[] simpleAsByteArray = simpleWord.getBytes(); for(byte b : simpleAsByteArray) { System.out.println(b); } // convert back from byte array to string String simpleWordFromByteArray = new String(simpleAsByteArray); System.out.println("simpleWordFromByteArray: " + simpleWordFromByteArray); System.out.println("--------------------------------------"); for(int i = 0; i < simpleWord.length(); i++) { char c = simpleWord.charAt(i); System.out.println("Char: " + c); } System.out.println("--------------------------------------"); String s = "This is a simple string"; // check if string is contained in string if(s.contains("is")) { System.out.println("'" + s + "' contains string 'is'" ); } System.out.println("--------------------------------------"); } }
Output:
Index of simple inside string 'a': 10
--------------------------------------
simpleString value: simple string
--------------------------------------
Word inside simpleString: simple
Word inside simpleString: string
--------------------------------------
Equal 'SIMPLE' and 'simple' with equalsIgnoreCase
--------------------------------------
115
105
109
112
108
101
simpleWordFromByteArray: simple
--------------------------------------
Char: s
Char: i
Char: m
Char: p
Char: l
Char: e
--------------------------------------
'This is a simple string' contains string 'is'
--------------------------------------