Showing posts with label string formats. Show all posts
Showing posts with label string formats. Show all posts

Tuesday, November 29, 2011

Java String valueOf example.


  1. /*
  2. Java String valueOf example.
  3. This Java String valueOf example describes how various java primitives and Object
  4. are converted to Java String object using String valueOf method.
  5. */
  6.  
  7. public class JavaStringValueOfExample{
  8.  
  9. public static void main(String args[]){
  10.  
  11. /*
  12.   Java String class defines following methods to convert various Java primitives to
  13.   Java String object.
  14.   1) static String valueOf(int i)
  15.   Converts argument int to String and returns new String object representing
  16.   argument int.
  17.   2) static String valueOf(float f)
  18.   Converts argument float to String and returns new String object representing
  19.   argument float.
  20.   3) static String valueOf(long l)
  21.   Converts argument long to String and returns new String object representing
  22.   argument long.
  23.   4) static String valueOf(double i)
  24.   Converts argument double to String and returns new String object representing
  25.   argument double.
  26.   5) static String valueOf(char c)
  27.   Converts argument char to String and returns new String object representing
  28.   argument char.
  29.   6) static String valueOf(boolean b)
  30.   Converts argument boolean to String and returns new String object representing
  31.   argument boolean.
  32.   7) static String valueOf(Object o)
  33.   Converts argument Object to String and returns new String object representing
  34.   argument Object.
  35.   */
  36.  
  37. int i=10;
  38. float f = 10.0f;
  39. long l = 10;
  40. double d=10.0d;
  41. char c='a';
  42. boolean b = true;
  43. Object o = new String("Hello World");
  44.  
  45. /* convert int to String */
  46. System.out.println( String.valueOf(i) );
  47. /* convert float to String */
  48. System.out.println( String.valueOf(f) );
  49. /* convert long to String */
  50. System.out.println( String.valueOf(l) );
  51. /* convert double to String */
  52. System.out.println( String.valueOf(d) );
  53. /* convert char to String */
  54. System.out.println( String.valueOf(c) );
  55. /* convert boolean to String */
  56. System.out.println( String.valueOf(b) );
  57. /* convert Object to String */
  58. System.out.println( String.valueOf(o) );
  59.  
  60. }
  61.  
  62. }
  63.  
  64. /*
  65. OUTPUT of the above given Java String valueOf Example would be :
  66. 10
  67. 10.0
  68. 10
  69. 10.0
  70. a
  71. true
  72. true
  73. Hello World
  74. */

Java String split example.


  1. /*
  2. Java String split example.
  3. This Java String split example describes how Java String is split into multiple
  4. Java String objects.
  5. */
  6.  
  7. public class JavaStringSplitExample{
  8.  
  9. public static void main(String args[]){
  10. /*
  11.   Java String class defines following methods to split Java String object.
  12.   String[] split( String regularExpression )
  13.   Splits the string according to given regular expression.
  14.   String[] split( String reularExpression, int limit )
  15.   Splits the string according to given regular expression. The number of resultant
  16.   substrings by splitting the string is controlled by limit argument.
  17.   */
  18.  
  19. /* String to split. */
  20. String str = "one-two-three";
  21. String[] temp;
  22.  
  23. /* delimiter */
  24. String delimiter = "-";
  25. /* given string will be split by the argument delimiter provided. */
  26. temp = str.split(delimiter);
  27. /* print substrings */
  28. for(int i =0; i < temp.length ; i++)
  29. System.out.println(temp[i]);
  30.  
  31. /*
  32.   IMPORTANT : Some special characters need to be escaped while providing them as
  33.   delimiters like "." and "|".
  34.   */
  35.  
  36. System.out.println("");
  37. str = "one.two.three";
  38. delimiter = "\\.";
  39. temp = str.split(delimiter);
  40. for(int i =0; i < temp.length ; i++)
  41. System.out.println(temp[i]);
  42.  
  43. /*
  44.   Using second argument in the String.split() method, we can control the maximum
  45.   number of substrings generated by splitting a string.
  46.   */
  47.  
  48. System.out.println("");
  49. temp = str.split(delimiter,2);
  50. for(int i =0; i < temp.length ; i++)
  51. System.out.println(temp[i]);
  52.  
  53. }
  54.  
  55. }
  56.  
  57. /*
  58. OUTPUT of the above given Java String split Example would be :
  59. one
  60. two
  61. three
  62. one
  63. two
  64. three
  65. one
  66. two.three
  67. */

Java String compare example.


  1. /*
  2. Java String compare example.
  3. This Java String compare example describes how Java String is compared with another
  4. Java String object or Java Object.
  5. */
  6.  
  7. public class JavaStringCompareExample{
  8.  
  9. public static void main(String args[]){
  10.  
  11. /*
  12.   Java String class defines following methods to compare Java String object.
  13.   1) int compareTo( String anotherString )
  14.   compare two string based upon the unicode value of each character in the String.
  15.   Returns negative int if first string is less than another
  16.   Returns positive int if first string is grater than another
  17.   Returns 0 if both strings are same.
  18.   2) int compareTo( Object obj )
  19.   Behaves exactly like compareTo ( String anotherString) if the argument object
  20.   is of type String, otherwise throws ClassCastException.
  21.   3) int compareToIgnoreCase( String anotherString )
  22.   Compares two strings ignoring the character case of the given String.
  23.   */
  24.  
  25. String str = "Hello World";
  26. String anotherString = "hello world";
  27. Object objStr = str;
  28.  
  29. /* compare two strings, case sensitive */
  30. System.out.println( str.compareTo(anotherString) );
  31. /* compare two strings, ignores character case */
  32. System.out.println( str.compareToIgnoreCase(anotherString) );
  33. /* compare string with object */
  34. System.out.println( str.compareTo(objStr) );
  35.  
  36. }
  37.  
  38. }
  39.  
  40. /*
  41. OUTPUT of the above given Java String compare Example would be :
  42. -32
  43. 0
  44. 0
  45. */

Java Search String using indexOf Example


  1. /*
  2.   Java Search String using indexOf Example
  3.   This example shows how we can search a word within a String object using
  4.   indexOf method.
  5. */
  6.  
  7. public class SearchStringExample {
  8.  
  9. public static void main(String[] args) {
  10. //declare a String object
  11. String strOrig = "Hello world Hello World";
  12.  
  13. /*
  14.   To search a particular word in a given string use indexOf method.
  15.   indexOf method. It returns a position index of a word within the string
  16.   if found. Otherwise it returns -1.
  17.   */
  18.  
  19. int intIndex = strOrig.indexOf("Hello");
  20.  
  21. if(intIndex == - 1){
  22. System.out.println("Hello not found");
  23. }else{
  24. System.out.println("Found Hello at index " + intIndex);
  25. }
  26.  
  27. /*
  28.   we can also search a word after particular position using
  29.   indexOf(String word, int position) method.
  30.   */
  31.  
  32. int positionIndex = strOrig.indexOf("Hello",11);
  33. System.out.println("Index of Hello after 11 is " + positionIndex);
  34.  
  35. /*
  36.   Use lastIndexOf method to search a last occurrence of a word within string.
  37.   */
  38. int lastIndex = strOrig.lastIndexOf("Hello");
  39. System.out.println("Last occurrence of Hello is at index " + lastIndex);
  40.  
  41. }
  42. }
  43.  
  44. /*
  45. Output of the program would be :
  46. Found Hello at index 0
  47. Index of Hello after 11 is 12
  48. Last occurrence of Hello is at index 12
  49. */

How to find the most appropriate Keywords?

  🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...