cancel
Showing results for 
Search instead for 
Did you mean: 

Word count in string

Former Member
0 Kudos

Can anybody suggest a solution for the following scenario ?

I want to either

a) transform a full name string which may contain multiple spaces to one containing single spaces i.e.

'Fred Joe Sid Andy Bloggs'

to become

'Fred Joe Sid Andy Bloggs'

or

b) count the component words in the string, the above full name string would return 5.

I've tried match_pattten with no luck.

There is an Oracle function , regexp_replace which would do for a) .

Thanks,

Eric.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Re above

'Fred Joe Sid Andy Bloggs' to become 'Fred Joe Sid Andy Bloggs'

former_member467822
Active Participant
0 Kudos

Same thing with the counting. Just create a function based on the following code and you can count the words.

$instring = 'Fred Joe Sid Andy Bloggs';

$cnt=1;

while (word($instring, $cnt) is not null)

$cnt=$cnt+1;

$cnt=$cnt-1;

print($cnt);

former_member467822
Active Participant
0 Kudos

Actually it's the way the HTML is displayed in the browser. If you look at the source you'll see the extra spaces there. Either way the code I posted should work.

Answers (4)

Answers (4)

Former Member
0 Kudos

That's worked fine.

Thanks

Former Member
0 Kudos

This wonderful forum software does just what I want, it reduces multiple spaces to single spaces.

Just try and imagine the first string with lots of spaces.

Thanks.

former_member467822
Active Participant
0 Kudos

In script it's pretty simple to remove the extra spaces. For example:

$instring = 'Fred Joe Sid Andy Bloggs';

print($instring);

while (index($instring, ' ', 1)>0)

$instring=replace_substr( $instring,' ',' ');

print($instring);

Just take the while and the line after and put them into a function and you can call it from a mapping.

Former Member
0 Kudos

Or, referring to above,

does anybody have the correct pattern string to match the following

'alpha_string||space_string||alpha_string||space_string||alpha_string||space_string||alpha_string'

i.e 'Fred Joe Sid Dave Bloggs'

Thanks again,

Eric.