Java Reference
In-Depth Information
wanted to add a space character in between the two strings, you could do so by passing
a space character as well as the string you want to append as follows:
String result = one.concat(" ".concat(two));
As you can see, having the ability to pass any string or combination of strings to the
concat() method makes it very useful. Because all of the string helper methods ac-
tually return copies of the original string with the helper method functionality applied,
you can pass strings calling other helper methods to concat() (or any other string
helper method) as well. Consider that you want to display the text "Hello Java"
rather than "Hello Java8" . The following combination of string helper methods
would allow you to do just that:
String one = "Hello";
String two = "Java8";
String result = one.concat(" ".concat(two.substring(0,
two.length()-1)));
The concatenation operator ( + ) can be used to combine any two strings. It is almost
thought of as a shorthand form of the concat() method. The last technique that is
demonstrated in solution #3 to this example is the use of StringBuffer , which is a
mutable sequence of characters, much like a string, except that it can be modified
through method calls. The StringBuffer class contains a number of helper meth-
ods for building and manipulating character sequences. In the solution, the append()
method is used to append two string values. The append() method places the string
that is passed as an argument at the end of the StringBuffer . For more information
regarding the use of StringBuffer , refer to the online documentation at ht-
tp://docs.oracle.com/javase/8/docs/api/java/lang/
StringBuffer.html .
3-6. Converting Strings to Numeric Val-
ues
Problem
Search WWH ::




Custom Search