Counting leading spaces

Started by Steven White on 22-Nov-2017/12:17:53-8:00
I want to count the leading spaces in a string. I can sort of do it, as shown in the sample below, but the core of the problem seems to be finding the first non-blank character. It seems like there must be a better way than specifying a list of 127 printable characters and then finding any one of them. I don't want to find "something," but rather "anything" as long as it is NOT a blank. Thank you. R E B O L [] ;;;;; 1 2 3 ;;;;; 123456789*123456789*123456789* STR: " XXXXXXXX YYYYY" NONBLANK: charset "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" FIRST-NONBLANK: index? find STR NONBLANK LEADING-SPACES: FIRST-NONBLANK - 1 REST-OF-STRING: at STR FIRST-NONBLANK print ["String value: " REST-OF-STRING] print ["Leading spaces: " LEADING-SPACES] halt
You can use COMPLEMENT: nonblanks: complement blanks: charset " " FIND/INDEX? is one way to go about things, can also PARSE: parse/all [ ; /ALL assuming Rebol 2 copy leading-spaces any blanks first-nonblank: copy rest-of-string to end ]
That should of course be:// parse/all str [...]
Note that charsets can also be defined like this: charset [#"0" - #"9" #"A" - #"Z" #"a" - #"z"]

Reply