Saturday, 7 January 2017

Write a java program to remove all white spaces from a String ?

Here we learn how to remove all the white spaces from string. For Example suppose we have string "Pushkar is a Good Boy" then output will come as "PushkarisaGoodBoy" .

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class RemoveAllSpace {
 static void method_1(String s1){
  
  System.out.println("OUTPUT M1 = "+s1.replaceAll("\\s", ""));
 }
 static void method_2(String s1){
  
  StringBuffer sb=new StringBuffer();
  for(int i=0;i<s1.length();i++){
   if(s1.charAt(i)!=' ' ){
    sb.append(s1.charAt(i));
   }
  }
  System.out.println("OUTPUT M2 = "+sb);
 }
 public static void main(String[] args) {
  RemoveAllSpace.method_1("Pushkar is a Good Boy");
  RemoveAllSpace.method_2("God Exists");
 }
}

Output:

OUTPUT M1 = PushkarisaGoodBoy
OUTPUT M2 = GodExists


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment