Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Little text functions
#1
Member function str.glue_strs
Code:
Copy      Help
function str'a str'b [str'separator]
;EXAMPLE:
;str a="a[]b[]c[]d[]e"
;str b="pru1[]pru2[]pru3[]pru4"
;str c.glue_strs(a b " - ")
;out c
ARRAY(str) aa(a) bb(b)
int l=iif(aa.len>bb.len aa.len bb.len)
ARRAY(str) t.create(l)
int i
for i 0 l
,t[i].from(aa[i] separator bb[i])
,err
,,t[i]=aa[i]
,,err
,,,t[i]=bb[i]
this=t
#2
Member function str.getmultiline
Code:
Copy      Help
function str'a pos [nc]
int nc1=nc
if(!nc) nc1=a.len
ARRAY(str) b(a)
int i
for i 0 b.len
,b[i].get(b[i] pos nc1)
this=b
this.trim


Member function str.leftmultiline
Code:
Copy      Help
function str'a nc
ARRAY(str) b(a)
int i
for i 0 b.len
,b[i].left(b[i] nc)
this=b
this.trim


Member function str.rightmultiline
Code:
Copy      Help
function str'a nc
ARRAY(str) b(a)
int i
for i 0 b.len
,b[i].right(b[i] nc)
this=b
this.trim


Member function str.insertmultiline
Code:
Copy      Help
function str'a from;; if from<0 add.
ARRAY(str) b(this)
int i
for i 0 b.len
,if(from<0) b[i].insert(a b[i].len)
,else b[i].insert(a from)
this=b
this.trim

note: everyone can add your own ...
#3
Function invert_array_str
Code:
Copy      Help
function ARRAY(str)&a
int i t(a.len-1) veces=t/2
for i 0 veces+1
,_s=a[i];a[i]=a[t];a[t]=_s
,t-1
ret 1
#4
Function decimal2roman
Code:
Copy      Help
function'str int'number
;EXAMPLE:
;out
;int i
;for i 1 1501
,;out "%i - %s" i decimal2roman(i)
,
str final
str tempr="M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I"
str tdecimal="1000,900,500,400,100,90,50,40,10,9,5,4,1"
ARRAY(str) sroman sdecimal
tok tempr sroman -1 ","
tok tdecimal sdecimal -1 ","
int inttemp intp i
if(number=0) ret "0"
if(number<0) ret "negative"
rep
,inttemp=number/val(sdecimal[intp])
,
,for i 0 inttemp
,,final+sroman[intp]
,
,number-(inttemp*val(sdecimal[intp]))
,intp+1
,
,if(number<=0) break
ret final


Function roman2decimal
Code:
Copy      Help
function str'a
a.ucase
int number
if(find(a "IV")>=0) number-2
if(find(a "IX")>=0) number-2
if(find(a "XL")>=0) number-20
if(find(a "XC")>=0) number-20
if(find(a "CD")>=0) number-200
if(find(a "CM")>=0) number-200

number+(a.findreplace("M")*1000)
number+(a.findreplace("D")*500)
number+(a.findreplace("C")*100)
number+(a.findreplace("L")*50)
number+(a.findreplace("X")*10)
number+(a.findreplace("V")*5)
number+(a.findreplace("I"))
ret number
#5
Function Proper
Code:
Copy      Help
function str&content
// convert to Proper Case (first letters only capitalized)
// example
//        str s = "jOHN q public"
//        Proper(s)    ;; s is now "John Q Public"

content.lcase
ARRAY(str) arr
int i nt
nt = tok(content arr -1 " ")
content = ""
for(i 0 nt)
,str c.get(arr[i] 0 1)
,c.ucase
,arr[i].set(c 0 1)
,content.from(content " " arr[i])
content.replacerx("^ ")
#6
Function join
Code:
Copy      Help
function'str ARRAY(str)&tokens [str'delimiter]
// the reverse of tok()
// concatenate an array of strings separated by delimiter
//
// example 1; tokenize then re-join
// str s1 = "IBM,MSFT,GE"
// ARRAY(str) A
// tok(s1 A ",")        ;; tokenize list into array A
// str s2 = join(A "+")    ;; re-join with new delimiter, s2 is "IBM+MSFT+GE"
//
// example 2; make a comma delimited, quote qualified list
// ARRAY(str) A2; A2.redim(4)
// A2[0] = "now is"
// A2[1] = "the time"
// A2[2] = "for all good"
// A2[3] = "men"
// str s3 = join(A2, "'',''")
// s3.from("''" s3 "''")    ;; "now is","the time","for all good","men"

int i; int n = tokens.len
str result
if (n == 0)                ;; default for empty array
,ret result

// start with the first token,
// then concatenate delimiter and subsequent tokens
result = tokens[0]
for i 1 n
,result.from(result, delimiter, tokens[i])
,
ret result
,


Forum Jump:


Users browsing this thread: 1 Guest(s)