PDA

View Full Version : Removing blanks from a string



JoeThompson
07-25-2002, 12:16 PM
Hi,

Is there a built in function to remove blanks from a string?
If not, how do most of you do it? Basically I want to validate that a value was typed in an edit field and that the user didn't just hit the space bar a couple of times.

Thank you,
Joe

SpdKlzU
07-25-2002, 12:52 PM
If you are using InstallScript you can just use the string token funtion (I think its called "StrToken") and use spaces (" ") as the delimeter. If you are using a DLL then it's easy to write your own little loop to go through and ignore the spaces as you copy the true characters of interest into another array.

Hope that helps,
Gim.

JoeThompson
07-25-2002, 03:34 PM
That's what I was affraid of. It seems like they should have standard Trim, TrimLeft, and TrimRight functions that remove blanks...

Thank you,
Joe

RobertDickau
07-26-2002, 09:59 AM
It's not documented, but the Example Custom Password Dialog project script that ships with Developer uses a function called SdRemoveEndSpace (pass in a variable containing a string, and in place it removes trailing spaces), which is presumably similar to your proposed TrimRight function...

JoeThompson
07-26-2002, 10:13 AM
Thanks for the tip. I just played around with it a little and it seems to remove leading blanks.
For example, I passed in " hello " and get back "hello ". I guess this would be alright if I just wanted to test for a complete blank entry...
Anyway, why isn't this documented? In the mean time I was able to find an example of stripping both leading and trailing blanks at the same time that I have been using.

Thank you,
Joe

olteanu
10-13-2002, 05:51 AM
Joe,
Will you tell me please where you found that ex?
Thanks,
dumitru olteanu

JoeThompson
10-14-2002, 01:29 PM
Hi Dumitru,

I don't know where I got it but here is the code I use:


function TrimString(aString)

BOOL done;
NUMBER startIndex;
NUMBER endIndex;
NUMBER aByte;

begin

startIndex = 0;
endIndex = StrLength(aString) - 1;

done = (startIndex > endIndex);

while (!done)

GetByte(aByte, aString, startIndex);

if (aByte = 32)
then
startIndex = startIndex + 1;
done = (startIndex > endIndex);
else
done = TRUE;
endif;

endwhile;

done = (startIndex > endIndex);

while (!done)

GetByte(aByte, aString, endIndex);

if (aByte = 32)
then
endIndex = endIndex - 1;
done = (startIndex > endIndex);
else
done = TRUE;
endif;

endwhile;

if (startIndex > endIndex)
then
aString = "";
else
StrSub(aString,
aString,
startIndex,
endIndex - startIndex + 1);
endif;

return 0;

end;


Hope this helps,
Joe

PeteDelgado
10-15-2002, 04:23 PM
The following code may help. Sorry if it's not well commented, but I have 20 libraries of functions that I created/used with IS5.5 that have external documentation.

-Pete


// Constant definitions
#define EAT_PRE 0
#define EAT_POST 1
#define EAT_ALL 2
#define EAT_BOTH 3


prototype StrEatWhite (BYREF STRING, INT);

function StrEatWhite (svString, nFlag)
NUMBER nIndex, nvLength, nChars;
STRING svTemp[1024];
begin

if ((nFlag = EAT_PRE) || (nFlag = EAT_BOTH))
then
nvLength = StrLength (svString);
nIndex=0;

while (((svString [nIndex]= ' ') || (svString [nIndex] = '\t')) && (nIndex < nvLength))
nIndex++;
endwhile;

StrSub (svTemp, svString, nIndex, nvLength);
svString = svTemp;
endif;

if ((nFlag = EAT_POST) || (nFlag = EAT_BOTH))
then
nvLength = StrLength (svString);
nIndex = nvLength -1;

while (((svString [nIndex]= ' ') || (svString [nIndex] = '\t')) && (nIndex >= 0))
svString [nIndex] = '\0';
nIndex--;
endwhile;

endif;

if (nFlag = EAT_BOTH)
then
// Since all processing has been done.
return TRUE;
endif;

if (nFlag = EAT_ALL)
then
nvLength = StrLength (svString);
nChars = 0;

for nIndex=0 to (nvLength -1)
if ((svString [nIndex] != ' ') && (svString [nIndex] != '\t'))
then
svTemp[nChars] = svString [nIndex];
nChars++;
endif;
endfor;

svString = svTemp;
return TRUE;
endif;

// BAD OPTION
return FALSE;
end;

RobertDickau
10-15-2002, 04:42 PM
If the target system has IE 4.0 or later installed, there's also the StrTrim API (for trimming characters on either end of the string, and not the center):


prototype BOOL Shlwapi.StrTrimA(BYREF STRING, BYVAL STRING);

function OnBegin( )
STRING strTrimMe, strTrimThis;
begin

strTrimThis = " "; // chars to trim from string
strTrimMe = " blanks on either side "; // string trimmed in place

StrTrimA(strTrimMe, strTrimThis);

MessageBox("[" + strTrimMe + "]", INFORMATION);

end;

olteanu
10-16-2002, 04:00 AM
Thank you Joe your code works fine.
Many thanks Pete and Robert I didn't try your solutions yet but I will do it
Thanks again
dumitru olteanu

Shuttledude
03-20-2007, 09:55 AM
For anyone wishing to solve this problem using the built-in StrGetTokens function, here is a code snippet:


// create a string that has leading blanks
// and some other blanks too
szString = " hello there world! 123";
szDelimiter = " ";
listofstrings = ListCreate(STRINGLIST);
StrGetTokens(listofstrings, szString, szDelimiter);
// This function populates a list with these tokens:
// hello
// there
// world!
// 123
//
// Let’s prove it worked with some message boxes
nResult = ListGetFirstString(listofstrings, szItem);
SprintfBox(INFORMATION, "First item",
"The first item is %s", szItem);
while (nResult = 0)
nResult = ListGetNextString(listofstrings, szItem);
if (nResult = 0) then
SprintfBox(INFORMATION, "First item",
"The next item is %s", szItem);
endif;
endwhile;

praveendanam
04-11-2007, 04:58 AM
hai,

Me too face the same problem. but seems strtrim is much good and simple
.

-praveen