https://docs.oracle.com/javase/tutorial/java/data/strings.html
String Length
public class StringDemo {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
// String Length
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
// Put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
}
for (int i = 0; i < len; i++) {
System.out.println(tempCharArray[i]);
}
// Reverse array of chars
for (int i = 0; i < len; i++) {
charArray[i] = tempCharArray[len -1 -i];
}
String reversePalindrome = new String(charArray);
System.out.println(reversePalindrome);
}
}
getChar
The Java String class getChars() method copies the content of this string into a specified char array. There are four arguments passed in the getChars() method. The signature of the getChars() method is given below:
public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex)
Concatenating Strings
The String
class includes a method for concatenating two strings:
string1.concat(string2);
This returns a new string that is string1 with string2 added to it at the end.
You can also use the concat()
method with string literals, as in:
"My name is ".concat("Rumplestiltskin");
Strings are more commonly concatenated with the +
operator, as in
"Hello," + " world" + "!"
which results in
"Hello, world!"
The +
operator is widely used in print
statements. For example:
String string1 = "saw I was "; System.out.println("Dot " + string1 + "Tod");