#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 are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu