2.c. Concatenation:
You can use the method concat to concatenate two strings
Code:
String s = s1.concat(s2);
It is also possible to use a plus sign + to perform a concatenation between two strings
Code:
String wd = message + " and " + "this is it";
2.d. Extraction of characters:
An object of the String class is immutable. Once it has been defined, its value can not be changed. However, we can assign a new string. If the String is defined as follows:
Code:
String message = "Warning!";
It may well change its content:
Code:
message = "new text";
The string "Warning!" is forgotten and the object message points to the new channel "new text". We can extract a substring using the method substring the String class. There are two versions in it
Code:
Public String substring(int beg, int end)
Which returns a new string starting at character position beg and goes up to character position end -1.
For example
Code:
message.substring txt = (0, 6);
returns the string "new".
And the second one
Code:
Public String substring(int start)
Which returns a new string starting at character position beginning and ending at the end of the initial string.
For example
Code:
txt = "first" + "new string". substring (8);
returns the string "first case".
2.e. Length of a string:
The length of a string is obtained using the method length ().
For example
Code:
ex.length () returns 11.
If you have a chain str. The method str.charAt (index) returns the specified character chain str. The parameter Index may have a value between 0 and str.length () - 1.
2.f. Miscellaneous Conversions:
Although the contents of a string can not be changed, it is possible to perform conversions by creating a new string. Methods toLowerCase and toUpperCase enable a channel respectively lowercase and uppercase. The method trim yields a new string without spaces at the beginning or end. The method replace (oldChar, newChar) can replace all the characters oldChar a chain of characters newChar.
2.g. Conversion of character and numeric values to strings:
The String class has several methods function valueOf() { [native code] } to convert a character, an array of characters and numeric values to strings. These methods have the same name, but differ in the type parameter supplied to them (char, char [], double, long, int, float, etc..)
Bookmarks