Category Archives: java example

Java program to remove string spaces

Java program to remove string spaces ?

Java program to remove leading and trailing spaces from string – the following java program or java source code to remove the space from string using toCharArray() and length with the help of for loop. The toCharArray() is used to convert the string to the character array and length is used to get the length of string.

java example

class A8

{

public static void main(String arr[])

{

int x[],i=0,j=0;

String s="        UmarKhan     ";

char ch[]=s.toCharArray();

int c=ch.length;

x=new int;

for(i=0;i<c;i++)

{


x[i]=ch[i];

}

for(i=0;i<c;i++)

{

if(x[i]!=32)

{

j=i;

break;

}

}

for(i=c-1;i>=0;i--)

{

if(x[i]!=32)

{

c=i;

break;

}

}

for(i=j;i<=c;i++)

System.out.print(ch[i]);

}

}

output

UmarKhan

The following java source code removes the leading and trailing spaces only not in between spaces.

 

For more c example, c tutorial orc tutorial and example PDF visit – java example

Do not forgot to like share and comment