Posts: 1,000
Threads: 253
Joined: Feb 2008
11-11-2021, 02:50 PM
(This post was last modified: 11-11-2021, 02:57 PM by TheVig.)
Hey folks...been a while and I hope y'all are doing swell!
Trying to camel case some text by removing spaces and capitalizing the letter that comes afterwords:
Function
camelCase
str Text="hello world"
Text.replacerx(" (.{1})" "\u$1")
out Text
I found this stackoverflow page that shows using the "\u" sort of thing, but in QM it outputs "hello\uworld" instead of "helloWorld"
https://stackoverflow.com/questions/2074...s/20742304
This works...but it's not very pretty:
Function
camelCase
str s="hello world"
REPLACERX r
r.frepl=&sub.replacerx_callback
s.replacerx(" (.{1})" &r)
out s
#sub replacerx_callback
function# REPLACERXCB&x
x.match.trim
x.match.ucase
Posts: 12,073
Threads: 140
Joined: Dec 2002
QM regex replacement does not support \u.
Could use callback, but problem with non-ASCII characters.
This function does not use regex.
Member function
str.TitleCase
;Makes the first character of every word uppercase.
;As word separators recognizes only ASCII whitespace (space, tab, newlines).
;Supports all Unicode word characters.
;EXAMPLE
;str s="hello world ąčę"
;s.TitleCase
;out s ;;Hello World Ąčę
if(!this.len) ret
BSTR b=this
int i
for i 0 b.len
,if i=0 or b[i-1]<=32
,,if b[i]>='a'
,,,_i=b[i]
,,,b[i]=CharUpperW(+_i)
this=b
Posts: 1,000
Threads: 253
Joined: Feb 2008
Yes that will work. Just title case it, then remove all the spaces. Nice.
Good to know QM does not support /u...I was thinking I was doing something wrong
Posts: 8
Threads: 1
Joined: Sep 2022
Running this causes this error:
Quote:'Error in <open ":3115: /268">test: 'this' can be used only in class member function
What further editing does this script require?
Posts: 1,336
Threads: 61
Joined: Jul 2006
09-11-2022, 01:08 AM
(This post was last modified: 09-11-2022, 01:18 AM by Kevin.)
You need to create a new member function
on qm editor window
click file, then new then new member function
name it str.TitleCase
then paste code from post above(click the copy link above the post and paste into this new function
if you are still unsure just download this file
open it and click import on qml fileviewer window
str.TitleCase.qml (Size: 3 KB / Downloads: 221)
Posts: 8
Threads: 1
Joined: Sep 2022
I imported the file, but nothing happens when I run the script.
what am I missing?
Posts: 1,336
Threads: 61
Joined: Jul 2006
09-11-2022, 03:25 AM
(This post was last modified: 09-11-2022, 03:30 AM by Kevin.)
you call it from another function or macro
the member function is not run using the run button. It is only called from code
example
Macro
Macro1
str s="hello world ąčę"
s.TitleCase
out s ;;Hello World Ąčę
Posts: 8
Threads: 1
Joined: Sep 2022
Ahhh, ok, now I understand. Thanks so much for the assist!!
Is there a similar function that would allow for just capitalizing the first word in a string?
Posts: 1,336
Threads: 61
Joined: Jul 2006
Macro
Macro3
str s="hello world ąčę"
int i=findc(s 32 0)
s.ucase(0 i)
out s
Posts: 8
Threads: 1
Joined: Sep 2022
Great stuff! Thank you, Kevin for all your help tonight.