Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 3 April 2011

How to Convert Character into a String in java

import java.io.*;
import java.lang.*;
/*
This program takes a character at the console and converts it into a string format using by the toString() method. This method converts the character into an integer type data. The character class wraps a value of the primitive type char in an object. The toString method returns a string object representing this character's values
*/


public class CharToString{
public static void main(String args[])throws IOException{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Character:");
String s = read.readLine();
char c = s.charAt(0);
System.out.println("Character value is " + c);
String s1 = Character.toString(c);
System.out.println("String value is " + s1);
}
}

Output of this program.

C:\corejava>java CharToString
Enter a Character:
R
Character value is R
String value is R
C:\corejava>

How to Compare String in java

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

How to Split a String in java Exmple explained

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

How to Convert String to Character in java Array Example

  1. /*
  2. Convert String to Character Array Example
  3. */
  4. public class StringToCharacterArrayExample {
  5. public static void main(String args[]){
  6. //declare the original String object
  7. String strOrig = "Hello World";
  8. //declare the char array
  9. char[] stringArray;
  10. //convert string into array using toCharArray() method of string class
  11. stringArray = strOrig.toCharArray();
  12. //display the array
  13. for(int index=0; index < stringArray.length; index++)
  14. System.out.print(stringArray[index]);
  15. }
  16. }

  1. /*
  2. Output of the program would be :
  3. Hello World
  4. */