Manipolazione delle stringhe in REXX 
Wednesday, 15 August 2012, 15:12 - Informatica, Programmazione, Programmazione strutturata, Linguaggi, REXX
Dividere una stringa in sottostringhe

split: /* (string, sep, stem[, limit]) */
  $sep = escape(arg(2), "'", "'")
  $l = cequ(arg(4, "E"), arg(4), -1)
  if $l = 1 then do
    $w = arg(1)
    $s = ""
  end
  else interpret "parse value arg(1) with $w'"$sep"'$s"
  $i = 1
  interpret arg(3)".$i=$w"
  do while $s <> ""
    $l = $l - 1
    if $l = 1 then do
      $w = $s
      $s = ""
    end
    else interpret "parse var $s $w'"$sep"'$s"
    $i = $i + 1
    interpret arg(3)".$i=$w"
  end
  interpret arg(3)".0=$i"
return 1



Eseguire l'escape dei caratteri

escape: procedure /* (string, chars, escape) */
  s = arg(1)
  sc = cequ(arg(2, "E"), arg(2), "'""\<>")
  ec = cequ(arg(3, "E"), arg(3), "\")
  r = ""
  l = length(s)
  do i = 1 to l
     c = substr(s, i, 1)
     if index(sc, c) > 0 then r = r !! ec
     r = r !! c
  end
return r



Convertire il testo in minuscolo

lCase: procedure /* (string) */
  s = arg(1)
  ccal = c2d("a")
  cczl = c2d("z")
  ccAU = c2d("A")
  ccZU = c2d("Z")
  ccd = ccal - ccAU
  r = ""
  do i = 1 to length(s)
    c = substr(s, i, 1)
    cc = c2d(c)
    if cc >= ccAU & cc <= ccZU then cc = cc + ccd
    r = r !! d2c(cc)
  end
return r



Convertire il testo in maiuscolo

uCase: procedure /* (string) */
  s = arg(1)
  parse upper var s s
return s



Wrappare un valore

formatArg:
  if datatype(arg(1)) = "NUM" then return arg(1)
return '"'escape(arg(1), '"', '"')'"'



Sostituire una sottostringa con un'altra

replace: procedure /* (string, find, replace) */
  parse arg s, f, r
  fl = length(f)
  x = ""
  lp = 1
  p = index(s, f)
  do while p > 0
    x = x !! substr(s, lp, p - lp) !! r
    lp = p + fl
    p = index(s, f, lp)
  end
return x !! substr(s, lp)



Verificare se una stringa inizia con una sottostringa

startsWith: procedure /* (string, s) */
return left(arg(1), length(arg(2))) = arg(2)



Commenti

Aggiungi commento
Commento non disponibile per questa notizia.