Remove White Spaces From String - Java

Input : OneSpace TwoSpaces  ThreeSpaces   FourSpaces    Tab        End
Output : OneSpaceTwoSpacesThreeSpacesFourSpacesTabEnd
String stringWithoutSpaces = inputString.replaceAll("\\s+", "");
or
     char[] charArray = inputString.toCharArray();
         
        String stringWithoutSpaces = "";
         
        for (int i = 0; i < charArray.length; i++) 
        {
            if ( (charArray[i] != ' ') && (charArray[i] != '\t') )
            {
                stringWithoutSpaces = stringWithoutSpaces + charArray[i];
            }
        }
         
        System.out.println("Input String : "+inputString);

Comments

Popular posts from this blog

Rest Assured

Sort (Bubble) - Java

Rotate the String from position - Java