Optimum Method to Concatenate Strings in Java

Manikanth Mani
Updated on 26-Feb-2020 06:02:08

222 Views

The Optimum Method to Concatenate Strings in Java is the concat() method.This method appends one String to the end of another. The method returns a String with the value of the String passed into the method, appended to the end of the String, used to invoke this method.ExampleLive Demopublic class Test {    public static void main(String args[]) {       String s = "Strings are immutable";       s = s.concat(" all the time");       System.out.println(s);    } } OutputStrings are immutable all the time

Why StringBuffer is Mutable in Java

Arjun Thakur
Updated on 26-Feb-2020 06:01:26

2K+ Views

We all know that the String class in Java is mutable i.e. once we create a String variable we cannot modify its data or do any manipulations.But, there may be scenarios where we need to modify the data of String variables. In such cases, we could use StringBuffer class.This class −is like a String, but can be modified. It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.Is safe for use by multiple threads.

Sort Records with Special Characters Like 2321-78-54-6

AmitDiwan
Updated on 26-Feb-2020 06:01:18

183 Views

To sort, use ORDER BY SUBSTRING(). Let us first create a table −mysql> create table DemoTable    -> (    -> Value varchar(40)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2321/78/54-6')    -> ; Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2321/78/54-8'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2321/78/54-5'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2321/78/54-9'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement ... Read More

Java Program to Concatenate String

Anjana
Updated on 26-Feb-2020 05:59:28

402 Views

The concat() method of the String class concatenates the specified string to the end of this string.ExampleLive Demoimport java.lang.*;    public class StringDemo {       public static void main(String[] args) {       // print str1       String str1 = "self";       System.out.println(str1);       // print str2 concatenated with str1       String str2 = str1.concat(" learning");       System.out.println(str2);       // prints str3 concatenated with str2(and str1)       String str3 = str2.concat(" center");       System.out.println(str3);    } }Outputself self learning self learning center

String Concatenation by Plus Operator

Ayyan
Updated on 26-Feb-2020 05:57:58

426 Views

You can concatenate strings using the ‘+’ operator of Java.ExampleLive Demopublic class Test {    public static void main(String args[]){       String st1 = "Hello";       String st2 = "How";       String st3 = "You";       String res = st1+st2+st3;       System.out.println(res);    } }OutputHelloHowYou

String Concatenation by concat() Method

Alankritha Ammu
Updated on 26-Feb-2020 05:57:18

249 Views

You can concatenate two strings using the concat() method of the String class. This class concatenates the specified string to the end of this string.ExampleLive Demopublic class Test {    public static void main(String args[]){       String str1 = "Krishna";       String str2 = "Kasyap";       System.out.println(str1.concat(str2));    } }OutputkrishnaKasyap

Java Program for String Concatenation

Akshaya Akki
Updated on 26-Feb-2020 05:56:38

213 Views

The concat() method of the String class concatenates the specified string to the end of this string.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       // print str1       String str1 = "self";       System.out.println(str1);             // print str2 concatenated with str1       String str2 = str1.concat(" learning");       System.out.println(str2);             // prints str3 concatenated with str2(and str1)       String str3 = str2.concat(" center");       System.out.println(str3);    } }Outputself self learning self learning center

Create String Object in Java

Moumita
Updated on 26-Feb-2020 05:55:48

2K+ Views

You can create a String by -Assigning a string value wrapped in " " to a String type variable.String message = "Hello Welcome to Tutorialspoint";Creating an object of the String class using the new keyword by passing the string value as a parameter of its constructor.String message = new String ("Hello Welcome to Tutorialspoint");Passing a character array to the String constructor.char arr[] = {'H','e','l','l','o'}; String message = new String(arr);

Order VARCHAR Records with Strings and Numbers in MySQL

AmitDiwan
Updated on 26-Feb-2020 05:52:01

563 Views

For this, use ORDER BY clause. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentCode varchar(20)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('101J'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('100A'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('100B'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('101S'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('103M'); Query OK, 1 row affected (0.15 ... Read More

Import java.lang.String Class in Java

Fendadis John
Updated on 26-Feb-2020 05:48:03

18K+ Views

To import any package in the current class you need to use the import keyword asimport packagename;ExampleLive Demoimport java.lang.String; public class Sample {    public static void main(String args[]) {       String s = new String("Hello");       System.out.println(s);    } }OutputHello

Advertisements