add to string java code example

Example 1: insert string in string java

String newString = originalString.substring(0, index + 1) 
                           + stringToBeInserted 
                           + originalString.substring(index + 1);

Example 2: java concatenate strings

public class Concat {
    String cat(String a, String b) {
        a += b;
        return a;
    }
}

Example 3: how do you combine 2 strings in java

-By  using (+) operator
-By  using concatenate method (concat()).
                      String strconcat3=strconcat2.concat(strconcat);

-By StringBuffer
-String strconcat= new StringBuilder().append("matt")
                                .append("damon").toString();
                                
-By StringBuilder
- String strconcat2= new StringBuffer()
                  .append("matt").append("damon").toString();

Tags:

Java Example