23
Apr
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
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
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
… Read more 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
… Read more You can get part of a string by following two methods:
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 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);
… 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 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
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:
… Read more 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
… Read more