Page 1 of 2

is there a way to convert "my house" to "My House" ?

Posted: 06 Feb 2017 18:46
by joeko
i am looking for an way to convert an variable to uppercase. every word in uppercase.
for example: name = My Name Is Mr Gorden and not my name is mr gordon

and so on. the first word or letter is not a problem but all first letters ?
maybe splitting the variable and convert ?
the numbers of words in this variable is not big but changing from 1 to 5.

someone know a way ?

Re: is there a way to convert "my house" to "My House" ?

Posted: 07 Feb 2017 11:58
by fagci
Try to make this with replaceAll with some magic pattern or split string to each word and apply toUpperCase() to each first letter, then ccncatenate all words

Re: is there a way to convert "my house" to "My House" ?

Posted: 07 Feb 2017 13:04
by joeko
thx for reply,
replaceall convert every letter ?

can you write here how i split, convert to uppercase and set to a new variable ?

Re: is there a way to convert "my house" to "My House" ?

Posted: 07 Feb 2017 16:43
by fagci

Code: Select all

t = 'test text';

r = split(t, '(?<=\W)|(?=\W)');

s = '';
for(word in r) {
  s = s + toUpperCase(left(word,1)) + toLowerCase(substring(word, 1));
}

log(t);
log(s);
There is some bug in my code, second word was not converted. Idea is clear, I think...

Re: is there a way to convert "my house" to "My House" ?

Posted: 07 Feb 2017 21:27
by joeko
hi,
thx for help

i tried this code but only the first word is like i need.
the idea is not really clear because i am new in this ...world.
do you know where the bug is in this code ?

what do this "r = split(t, '(?<=\W)|(?=\W)');"
splitting yes but what do the "W" ?

Re: is there a way to convert "my house" to "My House" ?

Posted: 08 Feb 2017 08:09
by fagci

Re: is there a way to convert "my house" to "My House" ?

Posted: 08 Feb 2017 08:49
by joeko
i see :-)
thx for the link and help

but i dont understand enough about it to find the bug in this code...
i get this message after convert from "test text" to "nullTest text"

what i have to change in the code to fix that so that i get "Test Text"?

Re: is there a way to convert "my house" to "My House" ?

Posted: 08 Feb 2017 17:05
by fagci

Code: Select all

t = 'test text';

r = split(t, '(?<=\\W)|(?=\\W)');

s = '';
for(word in r) {
  p = toUpperCase(left(word,1)) + toLowerCase(substring(word, 1));
  log(p);
  s = s + p;
}

log(t);
log(s);
You will escape special char by extra slash

Re: is there a way to convert "my house" to "My House" ?

Posted: 08 Feb 2017 18:46
by joeko
great it works :-)
but the code change also the numbers. is it possible that the numbers stay untouched ?

Re: is there a way to convert "my house" to "My House" ?

Posted: 08 Feb 2017 19:10
by fagci

Code: Select all

text = 'test text 123eee re456 345';

regexp = '(?<=\\W)|(?=\\W)';

words = split(text, regexp);

result = '';
for(word in words) {
  firstLetter = toUpperCase(left(word,1));
  rest = toLowerCase(substring(word,1));
  result = "{result}{firstLetter}{rest}";
}

log(text);
log(result);