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);
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
Post a Comment