String Practical FAQ
String Practical FAQ
Output :
Note :
In the second method, we remove all white spaces (including tab also) from a string
without using replaceAll() method. First we convert the given string to char array and
then we traverse this array to find white spaces. We append the characters which are not
the white spaces to StringBuffer object.
Here is the java program which uses both the methods to remove white spaces from a
string.
?
1
2
3 class RemoveWhiteSpaces
{
4 public static void main(String[] args)
5 {
6 String str = " Core Java jsp servlets jdbc struts hibernate spri
7
8 //1. Using replaceAll() Method
9
String strWithoutSpace = str.replaceAll("\\s", "");
10
11
System.out.println(strWithoutSpace); //Output : CoreJavajspservletsjd
12
13 //2. Without Using replaceAll() Method
14
15 char[] strArray = str.toCharArray();
16
17 StringBuffer sb = new StringBuffer();
18
19 for (int i = 0; i < strArray.length; i++)
{
20 if( (strArray[i] != ' ') && (strArray[i] != '\t') )
21 {
22 sb.append(strArray[i]);
23 }
}
24
25
System.out.println(sb); //Output : CoreJavajspservletsjdbcstrutshib
26 }
27 }
28
29
Flowchart :
?
1 StringBuffer sbf = new StringBuffer("MyJava");
2
3 System.out.println(sbf.reverse()); //Output : avaJyM
?
1 String str = "MyJava";
2
3 char[] strArray = str.toCharArray();
4
5 for (int i = strArray.length - 1; i >= 0; i--)
6 {
System.out.print(strArray[i]); //Output : avaJyM
7 }
8
?
1
static String recursiveMethod(String str)
2 {
3 if ((null == str) || (str.length() <= 1))
4 {
5 return str;
}
6
7
return recursiveMethod(str.substring(1)) + str.charAt(0);
8 }
9
This method takes the first character of a string (str.charAt(0)) and puts it at the end of
the string. And then calls itself on the remainder of the string (str.substring(1)). Finally
adds these two things to get the reverse of the passed string
(recursiveMethod(str.substring(1)) + str.charAt(0)). When the passed string is one
character or less (str.length() <= 1), it stops calling itself and just returns the string
passed.
If the “MyJava” is the string to reverse, then this method works like this.
After 6th call, it Stops calling itself. Because the length of passed string is 1. So, finally it
returns “avaJyM”.
Below is the Java program which reverses the string “MyJava” using all three above
methods.
?
1 public class ReverseTheString
{
2 public static void main(String[] args)
3 {
4 String str = "MyJava";
5
6 //1. Using StringBuffer Class
7
8 StringBuffer sbf = new StringBuffer(str);
9
System.out.println(sbf.reverse()); //Output : avaJyM
10
11
12
13
14 //2. Using iterative method
15
16 char[] strArray = str.toCharArray();
17
18 for (int i = strArray.length - 1; i >= 0; i--)
{
19 System.out.print(strArray[i]); //Output : avaJyM
20 }
21
22 System.out.println();
23
24 //3. Using Recursive Method
25
26 System.out.println(recursiveMethod(str)); //Output : avaJyM
}
27
28 //Recursive method to reverse string
29
30 static String recursiveMethod(String str)
31 {
32 if ((null == str) || (str.length() <= 1))
{
33 return str;
34 }
35
36 return recursiveMethod(str.substring(1)) + str.charAt(0);
37 }
38 }
39
40