Welcome! Log In Create A New Profile

Advanced

String Handling

Posted by Mark 
Mark
String Handling
April 03, 2008 10:45AM
Hi, I am a newbie with Windev so please excuse my ignorance, but I need to solve a problem & wondered whether anybody could help.

I have a string which contains the following data:-

mystring = "GB1234567890"

I need to determine how many alpha characters are contained in mystring, can anyone
advise how I would achieve this?

Tks
Paulo Oliveira
Re: String Handling
April 03, 2008 01:08PM
if you only nedd the number of char. use length.
Stefan Bentvelsen
Re: String Handling
April 03, 2008 02:13PM
Hi Mark,

the following code counts the alpha characters. The info() results 2 in this case.
Attention, it only counts uppercase characters now!

sHlp is string="AB1234567890"
i,j is int

FOR i=1 TO Length(sHlp)
IF sHlp[[i]]>="A" _AND_ sHlp[[i]]<="Z" THEN j++
END
Info(j)

Leo Voet
Re: String Handling
April 03, 2008 02:28PM
IF sHlp[[i]]>="A" _AND_ sHlp[[i]]<="Z" THEN j++
A lesser known, but quicker way to code :

IF "A"<= sHlp[[i]] <= "Z" THEN j++


... and it performance is (slightly) faster as well.

Leo.
Mark
Re: String Handling
April 03, 2008 03:23PM
Sefan,

Thanks very much, just the job!

Mark
Re: String Handling
April 03, 2008 03:25PM
Leo,

Thanks also, excellent neat that it's just one line of code!
Marc De Swert
Re: String Handling
April 03, 2008 04:02PM
or...
you can handle lower, upeer and numbers at the same time

sHlp is string="AB1234567DE Rs e890"
i,j,k are int
sTemp is int


FOR i=1 TO Length(sHlp)
stemp = Asc(sHlp[[i]])
IF ((stemp >= 65 AND stemp <= 90) OR (stemp >= 97 AND stemp <= 122)) THEN j++
IF (stemp >= 48 AND stemp <= 57) THEN k++
END
Info("ALPHA: " + j + " NUMB: " + K)


Greetings, Marc.
Rich
Re: String Handling
April 03, 2008 07:06PM
Why not just use IF NOT IsNumeric(sHlp[[i]] ) then k++
Marc De Swert
Re: String Handling
April 04, 2008 07:49AM
if you only test on "Not is numeric" then the space(s) and the / are also counted, the question was to count Alpha, in the example here you will count those chars in the result

sHlp is string="AB1234567DE/ Rs e890"
i,j,k , ni are int
sTemp is int


FOR i=1 TO Length(sHlp)
stemp = Asc(sHlp[[i]])
IF ((stemp >= 65 AND stemp <= 90) OR (stemp >= 97 AND stemp <= 122)) THEN j++
IF (stemp >= 48 AND stemp <= 57) THEN k++
IF NOT IsNumeric(sHlp[[i]]) THEN ni++
END
Info(" NotNumeric : " + ni + " ALPHA: " + j + " NUMB: " + K)

:xcool:
Rich
Re: String Handling
April 04, 2008 01:15PM
Marc,

Ok, I missed the point that there could be spaces and other characters in the string.sad smiley

The original question had just alpha and numeric.

Marc De Swert
Re: String Handling
April 04, 2008 02:19PM
No problemo

have a nice weekend

:xcool:
Carlo Hermus
Re: String Handling
April 06, 2008 07:48PM
Hi,

Why not

i, j is int
mystring = "Gb1234567890" 

FOR i = 1 TO Length(mystring )
	IF MatchRegularExpression(mystring [[i]], "[A-Za-z]") THEN j++
END
Info("Total letters in string: "+j)
Mark
Re: String Handling
April 07, 2008 10:38AM
Carlo,

Thanx for that suggestion, I'll try that out also.

Mark
Marc
Re: String Handling
April 07, 2008 10:39AM
Marc,

Thanx very much for your suggestion.

Mark
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: