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

Post your questions and help other users.

Moderator: Martin

joeko
Posts: 34
Joined: 17 Jan 2017 19:21

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

Post by joeko » 06 Feb 2017 18:46

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 ?

User avatar
fagci
Posts: 80
Joined: 03 Feb 2017 13:06
Location: Novosibirsk, RU
Contact:

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

Post by fagci » 07 Feb 2017 11:58

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
All systems nominal.

joeko
Posts: 34
Joined: 17 Jan 2017 19:21

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

Post by joeko » 07 Feb 2017 13:04

thx for reply,
replaceall convert every letter ?

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

User avatar
fagci
Posts: 80
Joined: 03 Feb 2017 13:06
Location: Novosibirsk, RU
Contact:

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

Post by fagci » 07 Feb 2017 16:43

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...
All systems nominal.

joeko
Posts: 34
Joined: 17 Jan 2017 19:21

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

Post by joeko » 07 Feb 2017 21:27

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" ?

User avatar
fagci
Posts: 80
Joined: 03 Feb 2017 13:06
Location: Novosibirsk, RU
Contact:

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

Post by fagci » 08 Feb 2017 08:09

All systems nominal.

joeko
Posts: 34
Joined: 17 Jan 2017 19:21

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

Post by joeko » 08 Feb 2017 08:49

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"?

User avatar
fagci
Posts: 80
Joined: 03 Feb 2017 13:06
Location: Novosibirsk, RU
Contact:

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

Post by fagci » 08 Feb 2017 17:05

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
All systems nominal.

joeko
Posts: 34
Joined: 17 Jan 2017 19:21

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

Post by joeko » 08 Feb 2017 18:46

great it works :-)
but the code change also the numbers. is it possible that the numbers stay untouched ?

User avatar
fagci
Posts: 80
Joined: 03 Feb 2017 13:06
Location: Novosibirsk, RU
Contact:

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

Post by fagci » 08 Feb 2017 19:10

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);
All systems nominal.

Post Reply