Skip to content

String Handling in Java | Substring, concat, Split | Examples

  • by

Java String is a very important class. The string is a sequence of characters. Once the String object is created, you can’t change the value of this object. Many ways are we can do String Handling in java. For example find Substring, String concatenation, String compression, Split string, etc.

String Handling in Java

Java String class is encapsulated under java.lang package. It differs from other programming languages- in C or C++, where (in C or C++) string is simply an array of char.

Basic String handling in java

Here is the string that can use and handle in Java for application and fulfill the requirements.

  • String variable
  • Find Substring
  • String concatenation
  • String compression
  • Split string
  • Reverse string

How to use String as a variable in java?

A direct way to create a string is:-

String greeting = "Hello world!";

Follow the link for more string java examples – Java String Class | Syntax | Methods | Examples

Find Substring | How to handle

You can check if a given string is a substring of another string. Use the “contains()” method and pass the value as a string (searching word). See below the example code of it.

String s = "hello world i am Programmer";
    if (s.contains("world")) {
        // This string contains "world"
    }

String concatenation | concat() method

Java concat() method concatenates the string to the end of the current string.

class StringConcat{  
 public static void main(String args[]){  
   String s1="Eye";  
   String s2="Hunts";  
   String s3=s1.concat(s2);  
   System.out.println(s3);
  }  
}  

Output: EyeHunts

String compression | equal() method

You can compare 2 strings by using the Java string equals() method. The return value will be true if equal and false for not equal.

Note: don’t use “==” Comparison Operator, it will check string object reference only not by value.

Another method can use the compareTo() method. If strings are equal then the result is 0.

class Teststringcompare{  
 public static void main(String args[]){  
   String s1="str";  
   String s2="str";  
   
   System.out.println(s1.equals(s2));//true  
   System.out.println(s1.compareTo(s2));//0  
 }  
}  

Split string in Java

Use the java split method to get the substring or split or char form String. 

public class SplitExample {
    public static void main(String args[]) {
        String str1 = "How to split Strings in Java";
        //splits the string where is space
        String[] strArray = str1.split("\\s");
        for (String w : strArray) {
            System.out.println(w);
        }
    }
}

Note: For more examples and complete tutorial on splitting string follow this link – Java string split Method | Regex With Space, Comma

String reverse in java

Here is the code: Using Java for loop, you can also use the StringBuilder reverse() method.

public class RevString {
    public static void main(String[] args) {
        Reverse("Canyon");        
    }

    public static void Reverse (String str) {
        int len = str.length();
        String rev="";

        for (int i = 0; i < len; i++) {
            rev = str.charAt(i) + rev;
        }
        System.out.println(rev);    
    }
}

Do comment if you have any doubts and suggestions on this topic.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All String Handling in Java is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *