public class RemoveSpaceFromString {
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
String str="";
System.out.println("enter the string");
str=s1.nextLine();
int l=str.length();
char ch[]=str.toCharArray();
for(int i=0;i<l;i++)
{
if(ch[i]==' ')
{
for(int k=i;k<l-1;k++)
{
ch[k]=ch[k+1];
}
l--;
i--;
}
}
String str2=new String(ch);
System.out.println(str2);
}
}
ouptput :
enter the string
my name is abc
mynameisabcccc
how to remove the extra 'c' from the end
c?str.replaceAll()...?replacemethod which does what you want.replaceAlluses regex. Your example doesn't need that,replace(" ", "")will do the same (it also replaces all occurrences of" "with""despite the lack ofAllsuffix).