Java String Example

1. Write a program to concat two string

Method name: String.concat();

public class Main {
    public static void main(String[] args) {
        String sentence1="Hello ";
        System.out.println(sentence1.concat("Java"));
    }

}
Output:
Hello Java

2. Write a program to find the length of a string

Method to find length of string: String.length();

public static void main(String[] args) {
        String sentence="Hello Java";
        int count=sentence.length();
        System.out.println(count);
    }
Output: 10

3. Write a Write a program to check if a keyword is contained within a string.

Method to found match word is: String.contains();

public static void main(String[] args) {
        String sentence="Hello Java";
        String findString = "Java";
        Boolean found=sentence.contains(findString);
        if(found){
            System.out.println("String found");
        }
        else{
            System.out.println("String not found");
        }
    }
Output:
Hello Java
Type a word to search:

C#
String not found
Java
String found

4. Write a program to find out if a string starts with and end with

Method: String.startsWith();

It will return the boolean value.

public class Main {
    public static void main(String[] args) {
        String sentence="Hello Java";
        System.out.println(sentence.startsWith("Hello"));
    }

}
Output:
true

5. Write a program to format DateTime

public class Main {
    public static void main(String[] args) {
        String format = "dd-MM-yyyy hh:mm:ss aa";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format); //passing format as constructor
        String date = simpleDateFormat.format(new Date());
        System.out.println(date);

    }
}
Output:
12-12-2020 04:06:07 PM

6. Write a program to check if a string variable is empty or not

Method to check if a string is empty or not: String.isEmpty();

public class Main {
    public static void main(String[] args) {
       String string1="Hello Java";
       String string2="";
       boolean str1=string1.isEmpty(); //expected: should be false
       boolean str2=string2.isEmpty(); //expected: should be true

       System.out.println(str1);
       System.out.println(str2);

    }

}
Output:
false
true

7. Write a program to replace a string

String replace method is: String.replace(“oldValue”,”newValue”);

public class Main {
    public static void main(String[] args) {
      String sentence1="Java programming is very hard";
      String newSentence=sentence1.replace("hard","easy");
      System.out.println(newSentence);
    }

}
Output:
Java programming is very easy

8. Write a program to return part of a string

You can get part of a string by following two methods:

  1. substring(index) : This is a method of removing string before the index position
  2. substring(0,index) : This is a method to return string from 0 index to its given index

Suppose, there is a string as, “Hello Java”

If you want to return only Hello, then you have to use substring(0,6). In this string, there are total 10 characters. substring(0,6) means, remove characters after 6th character. So it will return hello.

If you want to return Java, then you have to use substring(6). It will remove 1st 6 characters from the string and will return Java.

Program to evaluate above scenario:

public static void main(String[] args) {
      String s="Hello Java";
        System.out.println(s.substring(0,6));
        System.out.println(s.substring(6));
    }
Output:
Hello 
Java

9. Write a program to convert a string to upper and lower case

Method for convert to lower case:
String.toLowerCase();

Method for convert to UPPER case:

String.toUpperCase();

A simple Java program:

public class Main {
    public static void main(String[] args) {
        String s="Hello Java";
        System.out.println(s.toLowerCase()); //Convert to lower case
        System.out.println(s.toUpperCase()); //Convert to upper case
    }

}
Output:
hello java
HELLO JAVA

10. Write a program that will remove extra white space from a string

Method to remove extra white space:
String.trim();

A simple Java program:

public class Main {
    public static void main(String[] args) {
        String s = " Hello Java "; //adding white space before and after string
        System.out.println(s.trim());
    }

}
Output: 
Hello java //after removing white space
about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

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