Computing.Net > Forums > Programming > trimming spaces in a string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

trimming spaces in a string

Reply to Message Icon

Name: Rick Tran
Date: March 12, 2002 at 14:23:17 Pacific
Comment:

Is there a command, similar to ALLTRIM, that removes the blank spaces in a string.

ie. "123 456" --> "123456"

Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: what language??
Date: March 12, 2002 at 15:57:32 Pacific
Reply:

What are you programming in?


0

Response Number 2
Name: slash
Date: March 13, 2002 at 12:30:37 Pacific
Reply:

hi ,
it that is java that you are progarmming in , then the commmad is .trim() , and it will trim the spaces from the end .... i dont thing that there is something like this C .

slash


0

Response Number 3
Name: g
Date: March 13, 2002 at 14:08:37 Pacific
Reply:

Sorry slash, thats not correct. The .trim() function will only trim the whitespaces to the right and left of the string.. not the middle. To make this really simple.. convert the string to a character array, loop through the array one space at a time and if the character isnt a white space, copy it to another character array of equal size, if it is a whitespace, increment past it... when you are done, convert the character array back into a string. There may be a function out there that will do that, but I have never used it(Assuming your using java).


0

Response Number 4
Name: Apple
Date: March 13, 2002 at 14:09:39 Pacific
Reply:

ALLTRIM() only removes the spaces on the ends of a string. To remove spaces in the middle, you'll need to write your own.

this is a C version:

int i,j;

for( i = 0; string[ i ]; i++ ) {
if( string[ i ] == ' ' ) {
for( j = i; string[ j ]; j++ ) {
string[ j ] = string[ j+1 ];
}}}

This will work for the most part in java as well. I think the string implementation is slightly different, though and you can't assume that the string is terminated with a 0.


0

Response Number 5
Name: Guy
Date: March 13, 2002 at 19:16:31 Pacific
Reply:

And if it is Java, then using a StringTokenizer and a StringBuffer will probably be better (faster) than .trim().

Guy


0

Related Posts

See More



Response Number 6
Name: Rick Tran
Date: March 14, 2002 at 07:20:50 Pacific
Reply:

Sorry for the lack of information.
programming language is Visual Foxpro


0

Response Number 7
Name: Apple
Date: March 14, 2002 at 12:57:02 Pacific
Reply:

Whaaa.. How scary.
FoxPro string functions are best if you treat them a little like C strings. Using silly commands like MEMOLINE or whatever will actually slow you down.

LOCAL i
* tcString = some string
FOR i = LEN( tcString ) TO 1 STEP -1
ch = SUBSTR( tcString, i,1 )
IF( ch == ' ' ) THEN
tcString = SUBSTR( tcString, 1,i-1 ) + ;
SUBSTR( tcString,i+1 )
ENDIF
NEXT



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: trimming spaces in a string

removing letters in a string www.computing.net/answers/programming/removing-letters-in-a-string/9844.html

asp sql - sql query for last char in a string www.computing.net/answers/programming/asp-sql-sql-query-for-last-char-in-a-string/18767.html

To trim trailing spaces from a string www.computing.net/answers/programming/to-trim-trailing-spaces-from-a-string/20174.html