String Handling Methods with examples
1. char c=charAt(int index);
This method is used to return character at the specific index.
public class StringMethods { public static void main(String[] args) { String name = "deep singh"; char c = name.charAt(0); System.out.println(c); } }
Result: d
2. int c=codePointAt(int index);
This method is used to returns the Unicode of the character at the specified index.
public class StringMethods { public static void main(String[] args) { String name = "deep singh"; int c = name.codePointAt(0); System.out.println(c); } }
Result: 100
3. int c=codePointBefore(int index);
This method is used to returns the Unicode of the character before the specified index.
public class StringMethods { public static void main(String[] args) { String name = "deep singh"; int c = name.codePointBefore(1); System.out.println(c); } }
Result: 100
Note : In this case if you mention any index that is not exit then you get an Exception :
StringIndexOutOfBoundsException.
4. int c=codePointCount(int fromindex,int toindex);
This method is used to returns the Unicode in the specified text range of this String.
public class StringMethods { public static void main(String[] args) { String name = "deep singh"; int c = name.codePointCount(0,3); System.out.println(c); } }
Result: 3
5. int c=compareTo();
This method is used to Compares two strings lexicographically order.
public class StringMethods { public static void main(String[] args) { String a = "a"; String b = "b"; // if a is smaller than b result is negative number. int c1 = a.compareTo(b); // if a is greater than b result is positive number. int c2 = b.compareTo(a); // if both are equal then result is 0 number. int c3 = a.compareTo(a); System.out.println(c1+","+c2+","+c3); } }
Result: -1, 1, 0
6. int c=compareToIgnoreCase();
This method is used to Compares two strings lexicographically order ignoring the case.
public class StringMethods { public static void main(String[] args) { String a = "a"; String b = "B"; // if a is smaller than b result is negative number. int c1 = a.compareToIgnoreCase(b); // if a is greater than b result is positive number. int c2 = b.compareToIgnoreCase(a); // if both are equal then result is 0 number. int c3 = a.compareToIgnoreCase(a); System.out.println(c1+","+c2+","+c3); } }
Result: -1, 1, 0
7. String data=concat(String text);
This method is used to append two String with a new object.
public class StringMethods { public static void main(String[] args) { String a = "Deep"; String b = "Singh"; String t = a.concat(b); System.out.println(a); System.out.println(t); } }
Result:
Deep
DeepSingh
8. boolean data=contains(CharSequence text);
This method is used to check whether string contains sequence of character or not.
import java.io.Serializable; public class StringMethods { public static void main(String[] args) { String a = "hello i am a deep singh"; CharSequence cs = "deep"; System.out.println(a.contains(cs)); System.out.println(a.contains("am")); Serializable sl = "hello"; // you need to convert it into string because there is not method of type Serializable. System.out.println(a.contains(sl.toString())); } }
Result:
true
true
true
9. boolean data=contentEqual(CharSequence text);
OR
boolean data=contentEqual(StringBuffer text);
This method is used to check whether string contains the exact same sequence of character or not.
This method is overloaded type of CharSequence or StringBuffer.
JDK 1.4 (contentEquals (StringBuffer chars))
JDK 1.5 (contentEquals (CharSequence chars))
public class StringMethods { public static void main(String[] args) { String a = "deep singh"; CharSequence s = "deep singh"; StringBuffer sb = new StringBuffer("deep singh"); System.out.println(a.contentEquals(s)); System.out.println(a.contentEquals(sb)); } }
Result:
true
true
10. String data=copyValueOf(char []chars);
OR
String data=copyValueOf(char[] chars,int startindex,int length);
This method is used to copy char array in to String.
This is a static method so you need to call by the help of class name.
public class StringMethods { public static void main(String[] args) { char[] chars = { 'd', 'e', 'e', 'p', ' ', 's', 'i', 'n', 'g', 'h' }; String name = String.copyValueOf(chars); System.out.println(name); String anothername = String.copyValueOf(chars, 0, chars.length); System.out.println(anothername); } }
Result:
deep singh
deep singh
11. boolean data=endsWith(String text);
This method is used to check whether string ends with particular character or not.
public class StringMethods { public static void main(String[] args) { String name = "This is deep singh"; boolean check = name.endsWith("h"); System.out.println(check); } }
Result: true
12. boolean data=equals(Object obj);
This method is used to compare two string with exact match.
public class StringMethods { public static void main(String[] args) { String name = "hello"; boolean check = name.equals("hello"); System.out.println(check); } }
Result: true
13. boolean data=equalsIgnoreCase(String obj);
This method is used to compare two string with exact match with ignore the case.
public class StringMethods { public static void main(String[] args) { String name = "hello"; boolean check = name.equalsIgnoreCase("Hello"); System.out.println(check); } }
Result: true
14. String data=format(String format,Object...o);
OR
String data=format(Locale l,String format,Object...o);
This method is used to formatted the String with given Locale, Format and Arguments.
public class StringMethods { public static void main(String[] args) { // print hexadecimal type of float data System.out.println(String.format("%a", 3.14)); // 0x1.91eb851eb851fp1 // print boolean type of data System.out.println(String.format("%b", true)); // true // print character type of data System.out.println(String.format("%c", 'a')); // a // print integral(Decimal) type of data System.out.println(String.format("%d", 123457)); // 123457 // print float data in scientific notation (written in two parts) System.out.println(String.format("%e", 49.000000)); // 4.900000e+01 // print float type of data System.out.println(String.format("%f", 1.234574)); // 1.234574 // print hex type of data System.out.println(String.format("%h", 123457)); // 1e241 // print data in next line System.out.println(String.format("%n", 123457)); // new line // print octal type of data System.out.println(String.format("%o", 123457)); // 361101 // print String type of data System.out.println(String.format("%s", "Hello World")); // Hello World System.out.println(String.format("%tA", System.currentTimeMillis())); // Friday System.out.println(String.format("%tB", System.currentTimeMillis())); // March System.out.println(String.format("%tD", System.currentTimeMillis())); // 03/27/20 System.out.println(String.format("%tT", System.currentTimeMillis())); // 21:50:59 System.out.println(String.format("%tZ", System.currentTimeMillis())); // IST System.out.println(String.format("%tY", System.currentTimeMillis())); // 2020 System.out.println(String.format("%ta", System.currentTimeMillis())); // Fri System.out.println(String.format("%tb", System.currentTimeMillis())); // Mar System.out.println(String.format("%td", System.currentTimeMillis())); // 27 System.out.println(String.format("%tT", System.currentTimeMillis())); // 21:50:59 System.out.println(String.format("%tZ", System.currentTimeMillis())); // IST System.out.println(String.format("%tY", System.currentTimeMillis())); // 2020 } }
15. byte[] data=getBytes();
This method is used to convert string in to byte type of array.
public class StringMethods { public static void main(String[] args) { String name = "hello"; byte [] bytes = name.getBytes(); System.out.println(bytes); } }
Result: [B@1db9742
16. getChars();
This method is used to copy string in to char type of array.
public class StringMethods { public static void main(String[] args) { String name = "hello"; char[] chars = new char[name.length()]; name.getChars(0, name.length(), chars, 0); for (int i = 0; i < chars.length; i++) { System.out.print(chars[i]); } } }
Result: hello
17. int position=indexOf(int ch);
OR
int position=indexOf(String text);
OR
int position=indexOf(int ch,int index);
OR
int position=indexOf(String ch,int index);
This method is used to get position or index of first occurrence character.
public class StringMethods { public static void main(String[] args) { String name = "hi Sir"; int i = name.indexOf('i'); System.out.println(i); int j = name.indexOf("Sir"); System.out.println(j); int k = name.indexOf('i', 2); System.out.println(k); int l = name.indexOf("i", 1); System.out.println(l); } }
Result: 1 3 4 -1
18. String data=intern();
This method is used to return string from memory, if it is created by new keyword. It creates exact copy of heap string object in string constant pool.
public class StringMethods { public static void main(String[] args) { // using new keyword String name = new String("deep"); System.out.println(name); // deep System.out.println(name.intern()); // deep // because both address are different one inside heap area another inside constant pool. System.out.println(name == name.intern()); // false // but contents are equal System.out.println(name.equals(name.intern())); // true // using literal String anothername = "deep"; // because name points address of heap area by default System.out.println(anothername == name); // false // but as you know string objects are immutable so JVM will never create another object with same name System.out.println(anothername == name.intern()); // true } }
19. boolean status=isEmpty();
This method is used to check whether string is empty or not.
public class StringMethods { public static void main(String[] args) { String name = ""; System.out.println(name.isEmpty()); } }
Result: true
20. int position=lastIndexOf(int ch);
OR
int position=lastIndexOf(String text);
OR
int position=lastIndexOf(int ch,int pos);
OR
int position=lastIndexOf(String text,int pos);
This method is used to get position or index of last occurrence character.
public class StringMethods { public static void main(String[] args) { String name = "Deep Singh"; int i = name.lastIndexOf('e'); System.out.println(i); int j = name.lastIndexOf("e"); System.out.println(j); int k = name.lastIndexOf('e', 3); System.out.println(k); int l = name.lastIndexOf("e", 3); System.out.println(l); } }
Result: 2 2 2 2
2 comments:
Best sir best , First time I got full knowledge of string methods
Outstanding explanation sir... 👌
Post a Comment