To declare a string the syntax is as follows:
Syntax:
String string-name;
String-name = new String(“string-line”);
This can be written in one line as:
String string-name = new String(“string-line”);
Methods: For implement the operations on strings we have many methods available in java some of the as followings:
Methods working
str.length() returns the length of the string.
str1.concat(str2) concatenate the contents of string str1 and str2.
str2.equals(str1) it returns true if string2 and string1 are equals.
str.CharAt(location) it returns the character of the string which is specified.
str1.compareTo(str2) it returns positive if str1 > str2 and negative if str1 < str2 and returns 0 if both are equals.
str.trim() it removes the white spaces in the string.
str.toLowerCase it converts the string to the lower case.
str.toUpperCase it converts the string to the upper case.
obj.toString() it converts the object in the form of string.
String.valueOf(variable) it converts the value of variable in the form of string.