String class of Java

Java has string class and it has a lot of functions. However, it is common for interviewers to ask about implementation of different string functions. But if you want to know the available functions with the string class then you can go through this documentation. It lists the method and function that it will perform.
  1. String S = new String("This is a new is language");
  2. S.charAt(0); //Finds the character as location 0
  3. S.compareTo(S1); //It will compare the strings S and S1
  4. S.concat(S); //It will concat the string S to itself.
  5. If S = "xyz", then concat will result into xyzxyz
  6. S.contains("new"); //It will find out if the string S contains substring "new"
  7. S.contentEquals("This is new"); //It will find out if String S is equal to the inputted string.
  8. S.endsWith("uage"); //It will find out if the String S ends with 'uage'
  9. S.equals(S); //Will compare 2 strings.
  10. S.getClass().getName();
  11. S.hashCode(); //It will compute the hashcode of the string
  12. S.indexOf('T'); //It will find the index of character 'T'
  13. S.indexOf("This"); //It will find the index of String "this"
  14. S.indexOf("s", 4); //It will find location of character s from location 4.
  15. S.indexOf("is", 10); //It will find location of string 'is' starting from location 10.
  16. S.isEmpty(); //It find out if the string is empty or not
  17. S.lastIndexOf(s); //Will find the location of character s starting from end of string.
  18. S.lastIndexOf("is"); //Will find the location of string 'is' starting from end of string.
  19. S.lastIndexOf(s, 10);
  20. S.lastIndexOf("is",10);
  21. S.length(); //Find the length of the string
  22. S.replace(s, e); //It will replace character s with e in the string
  23. S.replace("new", "old"); //It will replace occurence of string new to old.
  24. S.replaceAll("is", "was"); //Will replace all
  25. S.replaceFirst("is", "was"); //Will only replace First occurence of string 'is'
  26. S.substring(10); //Find a substring from character 10
  27. S.substring(10, 16); //Find a substring from character 10 to 16
  28. S.toLowerCase(); //Converts all characters to lowercase
  29. S.toUpperCase(); //Converts all characters to uppercase
  30. S.trim(); //Trims the leading and trailing whitespaces;
  31. S.copyValueOf(c); //Creates a copy of string
  32. S.copyValueOf(c, 0, 5); //Creates a copy of string from character 0 to 5
How to convert a string to a character array:
  1. char[] c = new char[20];
  2. c = S.toCharArray(); //Converts a string to a character array
How to get character from specific locations in the character array
  1. char[] destChar = new char[10]; S.getChars(0, 10, destChar, 0); //It will get characters from location 0 to 10 into destination character array


No comments:

Post a Comment

NoSQL

This one is reviewed but I need to delete its copy from hubpages or somewhere NoSQL Data models: key-value  Aggregate model.  key or i...