13.6 C
Jaipur
Sunday, January 23, 2022

How to join a string in Java

Must read

In Java, strings are used to hold a series of characters and are considered as objects, and to join strings together we need to use a concatenate operator represented by the “+” sign with String.Join function. So in this article, we will discuss some examples to make you understand its functionality.

How to join a string in Java using a comma delimiter

So, we will start this code by creating a class with the name of ‘StringJoin’ as shown below

After declaring the main function, if you want to display anything on the screen then you can do that by using System.out.println() function:

System.out.println(“String join example 1”);

Now we will create an array with a string data type and provide some string values to it:

String[] countries={“USA”, “UK”, “Japan”, “Australia”};

In the above code, we have used the name of different countries as string values and then saved them in an array with the name of ‘countries’. So, now to join these string values together we need to use a ‘string.join’ function along with any character that you want to join with as shown below:

This means that we are going to join the above-mentioned string together and in between, there will be a ‘,’ delimiter operator and the result will be saved in the variable ‘s’. Now to implement this and everything together we need to use a concatenation operator (+):

System.out.println(“Countries are: “+ s);

So the full code implementation is shown below:

public class JoinString {
    public static void main(String[] args)
    {
        //String join example 1
System.out.println(“String join example 1”);
   //Declare some strings and initialize them
String[] countries={“USA”, “UK”, “Japan”, “Australia”};
        String s=String.join(“,”, countries);
System.out.println(“Countries are: “+ s);
     }
}

Now you need to open any text editor available in your Linux operating system and write a code inside it by typing:

Now you need to make sure that you have installed the java development kit (JDK) in your Linux operating system, otherwise, you won’t be able to execute any Java program. But if you didn’t install it then you can do that by typing:

$ sudo apt install defaultjdk

Next is to execute the code but before that, you need to compile it first by typing.

$ javac JoinString.java

$ java JoinString

How to join a string in Java using a dash delimiter

So, previously we have used the ‘,’ delimiter operator in between the string values now to add another delimiter value such as ‘-’ we need to make changes in one line, and the remaining code will remain the same compared to the previous example which is:

s=String.join(“-“, countries);

Now the full code of such a scenario is:

public class JoinString {
    public static void main(String[] args)
    {
        //String join example 2
System.out.println(“String join example 2”);
        //Declare some strings and initialize them
String[] countries={“USA”, “UK”, “Japan”, “Australia”};
System.out.println(“Strings are: “+string);
        String s=String.join(“-“, countries);
System.out.println(“Countries are: “+ s);
}
}

Just like the previous example, we need to first write and save the program in nano editor as shown below.

After that, you need to compile it first before its execution.

Conclusion

Strings are used to contain a sequence of characters in Java that are considered objects. To do so, use the string.join() operator followed by whatever character you want to join, and then use the concatenate operator (+) to combine the string values. We’ve gone through two distinct examples in this article to help you learn how to connect a string in Java.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article