Java 17 Recipes


-10. Concatenating Strings


Download 3.2 Mb.
Pdf ko'rish
bet57/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   53   54   55   56   57   58   59   60   ...   245
Bog'liq
Java 17 Recipes

3-10. Concatenating Strings
 Problem
There are various strings that you want to combine into one.
 Solution 1
If you want to concatenate strings onto the end of each other, use the concat() method. 
The following example demonstrates the use of the concat() method.
String one = "Hello";
String two = "Java17";
String result = one.concat(" ".concat(two));
The following is the result.
Hello Java
 Solution 2
Use the concatenation operator to combine the strings using shorthand. In the following 
example, a space character is placed between the two strings.
String one = "Hello";
String two = "Java17";
String result = one + " " + two;
Chapter 3 StringS


92
The following is the result.
Hello Java17
 Solution 3
Use StringBuilder or StringBuffer to combine the strings. The following example 
demonstrates the use of StringBuffer to concatenate two strings.
String one = "Hello";
String two = "Java17";
StringBuffer buffer = new StringBuffer();
buffer.append(one).append(" ").append(two);
String result = buffer.toString();
System.out.println(result);
The following is the result.
Hello Java17
 How It Works
The Java language provides a couple of different options for concatenating strings of text. 
Although none is better than the others, you may find one to work better in different 
situations. The concat() method is a built-in string helper method. It provides the 
ability to append one string onto the end of another, as demonstrated by solution 1 to 
this recipe. The concat() method accepts any string value; therefore, you can explicitly 
type a string value to pass as an argument if you want. As demonstrated in solution 1, 
simply passing one string as an argument to this method append it to the end of the 
string, which the method is called upon. However, if you wanted to add a space character 
between the two strings, you could do so by passing a space character and 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 the helper methods return copies 
of the original string with the helper method functionality applied, you can pass strings 
Chapter 3 StringS


93
calling other helper methods to concat() (or any other string helper method). Consider 
that you want to display the text "Hello Java" rather than "Hello Java17". The 
following combination of string helper methods would allow you to do just that.
String one = "Hello";
String two = "Java17";
String result = one.concat(" ".concat(two.substring(0, two.length()-2)));
The concatenation operator (+) can combine any two strings. It is almost thought 
of as a shorthand form of the concat() method. The last technique demonstrated 
in solution 3 to this example is the use of StringBuffer, a mutable sequence of 
characters, much like a string, except that it can be modified through method calls. The 
StringBuffer class contains several helper methods for building and manipulating 
character sequences. In the solution, the append() method append two string values. 
The append() method places the string that is passed as an argument at the end of the 
StringBuffer.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   53   54   55   56   57   58   59   60   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling