#1: Java Program to Remove non ASCII chars from String

  1. package com.instanceofjava;
  2.  
  3. class RemoveNonASCIIString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance��of��java";
  7.   System.out.println(str);
  8.  
  9.   str = str.replaceAll("[^\\p{ASCII}]", "");
  10.  
  11.   System.out.println("After removing non ASCII chars:");
  12.  
  13.   System.out.println(str);
  14. }
  15. }
Output:
  1. Instance��of��java
  2. After removing non ASCII chars:
  3. Instanceofjava


#2: Java Program to Remove multiple spaces in a string

  1. package com.instanceofjava;
  2.  
  3. class RemoveNonASCIIString{
  4.  
  5. public static void main(String [] args){ 

  6.   String str = "Instance  of    java";
  7.   StringTokenizer st = new StringTokenizer(str, " ");
  8.  
  9.   StringBuffer sb = new StringBuffer();
  10.  
  11.   while(st.hasMoreElements()){
  12.          sb.append(st.nextElement()).append(" ");
  13.   }
  14.  
  15.      System.out.println(sb.toString().trim());
  16.     
  17.     }
  18. }
  19. }


Output:
  1. Instance of java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu