Typically String.split() or StringTokenizer class is user to break up a string into tokens. Problem with these methods is that they do not handle quoted text as you may require.
For example, consider the following string:
The mans name was "Big Fred"
Using split() or StringTokenizer on this would give us 6 tokens: (The) (mans) (name) (was) (“Big) (Fred”). Typically this is not what we want.
This is where the StreamTokenizer class comes in handy as it gives better control over the parsing process including identifying quoted text. The following example shows its usage:
String s = "The mans name was \"Big Fred\"";
StreamTokenizer st = new StreamTokenizer(new StringReader(s));
st.quoteChar('"');
while (st.nextToken() != StreamTokenizer.TT_EOF) {
System.out.println(st.sval);
}
Now we get the 5 tokens as required: (The) (mans) (name) (was) (Big Fred)
written by objects
\\ tags: regex, split, StreamTokenizer, string, StringTokenizer
Java Strings are immutable (cannot be changed) so reversing a String requires creating a new String.
A loop can be used to build the reversed string by adding the characters from original string in reverse order, but the StringBuilder class provides a reverse() method to do it for us.
The following example shows its usage.
String s = "The string to be reversed";
StringBuilder sb = new StringBuilder(s);
sb.reverse();
String reversed = sb.toString();
written by objects
\\ tags: reverse, string, StringBuilder
The Arrays class has a set of helper toString methods for converting an array to a string representation.
String representation = Arrays.toString(array);
This will work for all array types. For Objects it uses the toString() method of the type to convert array elements.
If you need more control how the array is represented then you will need to implement the conversion yourself using a loop.
written by objects
\\ tags: array, arrays, convert, string