Category Archive : String Examples

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

Read more

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
Read more


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
Read more