Sei sulla pagina 1di 12

Write a String Reverser(and use Recursion!

public interface Reverser {


public String reverse(String str);
}
public class JdkReverser implements Reverser {
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return new StringBuffer(str).reverse().toString();
}
}
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);

}
  Java String valueOf example.
 This Java String valueOf example describes how various java primitives and
Object
 are converted to Java String object using String valueOf method.
 */
  
 public class JavaStringValueOfExample{
  
 public static void main(String args[]){
  
 /*
   Java String class defines following methods to convert various Java
primitives to
   Java String object.
   1) static String valueOf(int i)
   Converts argument int to String and returns new String object
representing
   argument int.
   2) static String valueOf(float f)
   Converts argument float to String and returns new String object
representing
   argument float.
   3) static String valueOf(long l)
   Converts argument long to String and returns new String object
representing
   argument long.
   4) static String valueOf(double i)
   Converts argument double to String and returns new String object
representing
   argument double.
   5) static String valueOf(char c)
   Converts argument char to String and returns new String object
representing
   argument char.
   6) static String valueOf(boolean b)
   Converts argument boolean to String and returns new String object
representing
   argument boolean.
   7) static String valueOf(Object o)
   Converts argument Object to String and returns new String object
representing
   argument Object.
   */
  
 int i=10;
 float f = 10.0f;
 long l = 10;
 double d=10.0d;
 char c='a';
 boolean b = true;
 Object o = new String("Hello World");
  
 /* convert int to String */
 System.out.println( String.valueOf(i) );
 /* convert float to String */
 System.out.println( String.valueOf(f) );
 /* convert long to String */
 System.out.println( String.valueOf(l) );
 /* convert double to String */
 System.out.println( String.valueOf(d) );
 /* convert char to String */
 System.out.println( String.valueOf(c) );
 /* convert boolean to String */
 System.out.println( String.valueOf(b) );
 /* convert Object to String */
 System.out.println( String.valueOf(o) );
  
 }
  
 }
  
 /*
 OUTPUT of the above given Java String valueOf Example would be :
 10
 10.0
 10
 10.0
 a
 true
 true
 Hello World
 */

1. /*
2. Check if string contains valid number example.
3. This example shows how to check if string contains valid number
4. or not using parseDouble and parseInteger methods of
5. Double and Integer wrapper classes.
6. */
7.  
8. public class CheckValidNumberExample {
9.  
10. public static void main(String[] args) {
11.  
12. String[] str = new String[]{"10.20", "123456",
"12.invalid"};
13.  
14. for(int i=0 ; i < str.length ; i ++)
15. {
16.  
17. if( str[i].indexOf(".") > 0 )
18. {
19.  
20. try
21. {
22. /*
23. * To check if the number is
valid decimal number, use
24. * double parseDouble(String
str) method of
25. * Double wrapper class.
26. *
27. * This method throws
NumberFormatException if the
28. * argument string is not a
valid decimal number.
29. */
30. Double.parseDouble(str[i]);
31. System.out.println(str[i] + " is
a valid decimal number");
32. }
33. catch(NumberFormatException nme)
34. {
35. System.out.println(str[i] + " is
not a valid decimal number");
36. }
37.  
38. }
39. else
40. {
41. try
42. {
43. /*
44. * To check if the number is
valid integer number, use
45. * int parseInt(String str)
method of
46. * Integer wrapper class.
47. *
48. * This method throws
NumberFormatException if the
49. * argument string is not a
valid integer number.
50. */
51.  
52. Integer.parseInt(str[i]);
53. System.out.println(str[i] + " is
valid integer number");
54. }
55. catch(NumberFormatException nme)
56. {
57. System.out.println(str[i] + " is
not a valid integer number");
58. }
59. }
60. }
61.  
62. }
63. }
64.  
65. /*
66. Output would be
67. 10.20 is a valid decimal number
68. 123456 is valid integer number
69. 12.invalid is not a valid decimal number
70. */

Convert String to Character Array Example


1. /*
2.  Convert String to Character Array Example
3.  This example shows how to convert a given String object to an array
4.  of character
5.  */
6.  
7. public class StringToCharacterArrayExample {
8.  
9. public static void main(String args[]){
10. //declare the original String object
11. String strOrig = "Hello World";
12. //declare the char array
13. char[] stringArray;
14.  
15. //convert string into array using toCharArray() method of string
class
16. stringArray = strOrig.toCharArray();
17.  
18. //display the array
19. for(int index=0; index < stringArray.length; index++)
20. System.out.print(stringArray[index]);
21.  
22. }
23.  
24. }
25.  
26. /*
27. Output of the program would be :
28. Hello World
29. */

Java Search String Example


 *
   Java Search String Example
   This example shows how we can search a word within a String object using
   indexOf method.
 */
  
 public class SearchStringExample {
  
 public static void main(String[] args) {
 //declare a String object
 String strOrig = "Hello world Hello World";
  
 /*
   To search a particular word in a given string use indexOf method.
   indexOf method. It returns a position index of a word within the
string
   if found. Otherwise it returns -1.
   */
  
 int intIndex = strOrig.indexOf("Hello");
  
 if(intIndex == - 1){
 System.out.println("Hello not found");
 }else{
 System.out.println("Found Hello at index " + intIndex);
 }
  
 /*
   we can also search a word after particular position using
   indexOf(String word, int position) method.
   */
  
 int positionIndex = strOrig.indexOf("Hello",11);
 System.out.println("Index of Hello after 11 is " + positionIndex);
  
 /*
   Use lastIndexOf method to search a last occurrence of a word within
string.
   */
 int lastIndex = strOrig.lastIndexOf("Hello");
 System.out.println("Last occurrence of Hello is at index " +
lastIndex);
  
 }
 }
  
 /*
 Output of the program would be :
 Found Hello at index 0
 Index of Hello after 11 is 12
 Last occurrence of Hello is at index 12
 */

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 String endsWith Example


1. /*
2.   String endsWith Example
3.   This example shows how to check if a particular string is ending with
4.   a specified word.
5. */
6. public class StringEndsWithExample {
7.  
8. public static void main(String[] args) {
9.  
10. //declare the original String
11. String strOrig = "Hello World";
12.  
13. /*
14.   check whether String ends with World or not.
15.   Use endsWith method of the String class to check the same.
16.   endsWith() method returns true if a string is ending with a given
word
17.   otherwise it returns false
18.   */
19. if(strOrig.endsWith("World")){
20. System.out.println("String ends with World");
21. }else{
22. System.out.println("String does not end with World");
23. }
24.  
25. }
26. }
27.  
28. /*
29. Output of the program would be :
30. String ends with World
31. */

Java String Length Example


1. /*
2.   Java String Length Example
3.   This example shows how to get a length of a given String object.
4. */
5.  
6. public class StringLengthExample {
7.  
8. public static void main(String[] args) {
9. //declare the String object
10. String str = "Hello World";
11.  
12. //length() method of String returns the length of a String.
13. int length = str.length();
14. System.out.println("Length of a String is : " + length);
15. }
16. }
17.  
18. /*
19. Output of a program would be:
20. Length of a String is : 11
21. */

Java String Replace Example


1. /*
2. Java String replace example.
3. This Java String Replace example describes how replace method of java
String class
4. can be used to replace character or substring can be replaced by new
one.
5. */
6.  
7. public class JavaStringReplaceExample{
8.  
9. public static void main(String args[]){
10.  
11. /*
12.   Java String class defines three methods to replace character or
substring from
13.   the given Java String object.
14.   1) String replace(int oldChar, int newChar)
15.   This method replaces a specified character with new character and
returns a
16.   new string object.
17.   2) String replaceFirst(String regularExpression, String newString)
18.   Replaces the first substring of this string that matches the given
regular
19.   expression with the given new string.
20.   3) String replaceAll(String regex, String replacement)
21.   Replaces the each substring of this string that matches the
22.   given regular expression with the given new string.
23.   */
24.  
25. String str="Replace Region";
26.  
27. /*
28.   Replaces all occourances of given character with new one and
returns new
29.   String object.
30.   */
31. System.out.println( str.replace( 'R','A' ) );
32.  
33. /*
34.   Replaces only first occourances of given String with new one and
35.   returns new String object.
36.   */
37. System.out.println( str.replaceFirst("Re", "Ra") );
38.  
39. /*
40.   Replaces all occourances of given String with new one and returns
41.   new String object.
42.   */
43. System.out.println( str.replaceAll("Re", "Ra") );
44.  
45. }
46.  
47. }
48.  
49. /*
50.  
51. OUTPUT of the above given Java String Replace Example would be :
52.  
53. Aeplace Aegion
54. Raplace Region
55. Raplace Ragion
56.  
57. */

Java String Reverse Example


 /*
  Java String Reverse example.
  This example shows how to reverse a given string
 */
  
 public class StringReverseExample {
  
 public static void main(String args[]){
 //declare orinial string
 String strOriginal = "Hello World";
 System.out.println("Original String : " + strOriginal);
  
 /*
   The easiest way to reverse a given string is to use reverse()
   method of java StringBuffer class.
   reverse() method returns the StringBuffer object so we need to
   cast it back to String using toString() method of StringBuffer
   */
  
 strOriginal = new StringBuffer(strOriginal).reverse().toString();
  
 System.out.println("Reversed String : " + strOriginal);
 }
  
 }
  
 /*
 Output of the program would be :
 Original String : Hello World
 Reversed String : dlroW olleH
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 startsWith Example


    String startsWith Example
   This example shows how to check if a particular string is starting with
   a specified word.
 */
  
 public class StringStartsWithExample {
  
 public static void main(String[] args) {
  
 //declare the original String
 String strOrig = "Hello World";
  
 /*
   check whether String starts with Hello or not.
   Use startsWith method of the String class to check the same.
   startsWith() method returns true if a string is starting with a given
word
   otherwise it returns false
   */
 if(strOrig.startsWith("Hello")){
 System.out.println("String starts with Hello");
 }else{
 System.out.println("String does not start with Hello");
 }
  
 }
 }
  
 /*
 Output of the program would be :
 String starts with Hello
 */
Remove leading and trailing space from
String example
 *
 Remove leading and trailing space from String example.
 This example shows how to remove leading and trailing space
 from string using trim method of Java String class.
 */
  
 public class RemoveLeadingTrailingSpace {
  
 public static void main(String[] args) {
  
 String str = " String Trim Example ";
  
 /*
 * To remove leading and trailing space from string use,
 * public String trim() method of Java String class.
 */
  
 String strTrimmed = str.trim();
  
 System.out.println("Original String is: " + str);
 System.out.println("Removed Leading and trailing space");
 System.out.println("New String is: " + strTrimmed);
 }
 }
  
 /*
 Output would be
 Original String is: String Trim Example
 Removed Leading and trailing space
 New String is: String Trim Example
 */

Potrebbero piacerti anche