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



The following two tabs change content below.
Umar Farooque Khan is Software developer/ web developer and professional blogger serving Worldwide on freelancer and ODesk platform to show the capability of his skills He is the founder of pTutorial.com. The main purpose of this site is sharing and exchanging of I.T. Knowledge.

Latest posts by Umar Farooque Khan (see all)