String Class is a widely used class in java. String Class represents a bunch of characters and is similar to a character array. String class has almost a dozen constructors and can be used to creat
Difference String StringBuffer and StringBuilder
We will see the differences between String, StringBuffer and StringBuilder. The main differences are related to thread safe nature and performance. Difference String StringBuffer and StringBuilder
String.format java example
In this post we will see String.format java examples. Format method in String returns a formatted string using the specified format string and arguments.
1 |
public static String format(String format,Object... args) |
String trim example
String trim method is used to remove the leading and trailing white spaces in a String.
1 |
public String trim() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject; public class StringTrimExample { public static void main(String[] args) { String testStr1 = " Make me Slim trim "; System.out.println("Original String ::" + testStr1); System.out.println("Original String length ::" + testStr1.length()); System.out.println("----------------------------------"); System.out.println("After trimming ::"+ testStr1.trim()); System.out.println("After trimming String length ::"+ testStr1.trim().length()); } } |
String concat example
Concat method of String in java is used to add a new String at the end of the original String.
1 |
public String concat(String str) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.kscodes.sampleproject; public class StringConcatExample { public static void main(String[] args) { String testStr1 = "This is test String"; String testStr2 = " Used for Concat Example."; System.out.println("After 2 String concat::" + testStr1.concat(testStr2)); System.out.println("Adding 1 more to concat::" + testStr1.concat(testStr2).concat("Added more to this String")); } } |
String endsWith example
String endsWith is used to check if a String ends with the given suffix.
1 |
public boolean endsWith(String suffix) |
String startsWith example
String startsWith method is used to check if a String starts with a specified substring. Startswith method has 2 variations and we will see both these.
1 2 |
public boolean startsWith(String prefix) public boolean startsWith(String prefix,int toffset) |
String replace methods example
String replace methods are used to replace a part of string with a new string. We have 4 variations for replace in String
1 2 3 4 |
String replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) String replaceAll(String regex, String replacement) String replaceFirst(String regex, String replacement) |
String lastIndexOf example
String lastIndexOf returns the index of the last occurrence of the given String for specified conditions searching backward. String lastIndexOf method can be used with multiple types of parameters
String indexOf example
String indexOf returns the index of the given String for specified conditions. String indexOf method can be used with multiple types of parameters as shown below
1 2 3 4 |
public int indexOf(int ch) public int indexOf(int ch,int fromIndex) public int indexOf(String str) public int indexOf(String str,int fromIndex) |