HP Forums

Full Version: String Library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Bonjour
Je vous propose une petite bibliothéque de fonctions traitant des chaînes de caractères.
Un fichier tetxe vous en détaille le contenu.
Espérant que cela vous sera utile.
Merci de vôtre attention.

Hello
I propose a small library of functions dealing with strings.
A Tetxe you file details the content .
Hoping that it will help.
Thanks for your attention.
Bonjour
Je vous propose une nouvelle fonction à ajouter à la library:

Hello
I propose a new function to add to the library :

Code:
EXPORT EXTRACT(s,o)
//string,char or {char,num}
BEGIN
 LOCAL n:=-1,p,c;
 IF TYPE(o)==6 THEN
  c:=o(1);n:=o(2);
 ELSE
  c:=o;
 END;
 c:=typara(c);p:=INSTRING(s,c);
 n:=IFTE(n<0,DIM(s)-p+1,n);
 IF p==0 OR n==0 THEN
  "";
 ELSE
  MID(s,p,n);
 END;
END;
exemple:
example:
EXTRACT("250_km","_") -> "_km"
EXTRACT("250_km de bouchons",{"_",3}) -> "_km"
Bonjour
Les fonctions LFORMAT ET RFORMAT ne fonctionnent pas correctement.
Exemple :
LFORMAT(16,2,"0") renvoie "0016" au lieu de "16".
Ceci est du à un bug de la fonction MAKELIST.
Voici une version corrigée de setstr qui est utilisée par ces deux fonctions:

Hello
The LFORMAT AND RFORMAT functions do not work properly.
Example :
LFORMAT (16,2,"0" ) returns " 0016 " instead of "16" .
This is due to a bug in the MAKELIST function.
Here is a corrected version of setstr that is used by these two functions:

Code:

setstr(c,n)
BEGIN
 LOCAL i,r;
 c:=typara(c);r:="";
 FOR i FROM 1 TO n
  r:=r+c;
 END;
 r;
END;
Sorry
Another function: replace a placeholder in a string with another
e.g. SREPL("Hello %w", "%w", "world") -> "Hello world"

Code:
EXPORT SREPL(str, key, val)
BEGIN
  LOCAL p;
  p := INSTRING(str, key);
  CASE
    IF p == 0 THEN  // Not found
      RETURN str;
    END;
    IF str == key THEN  // Placeholder is whole string
      RETURN val;
    END;
    IF p == 1 THEN  // Placeholder at start
      RETURN val + MID(str, DIM(key)+1);
    END;
    IF p == (DIM(str)-DIM(key)+1) THEN  // Placeholder at end
      RETURN LEFT(str, p-1) + val;
    END;
    DEFAULT RETURN LEFT(str, p-1) + val + MID(str, p+DIM(key));
  END;
END;
Bonjour

Sauf erreur, la fonction intégrée REPLACE permet cela.
Merci de vôtre intêret.

Hello

Unless you make a mistake, the built-in REPLACE function allows this.
Thanks for your interest.
(03-20-2017 08:39 PM)Tyann Wrote: [ -> ]Bonjour

Sauf erreur, la fonction intégrée REPLACE permet cela.
Merci de vôtre intêret.

Hello

Unless you make a mistake, the built-in REPLACE function allows this.
Thanks for your interest.

From the User Guide:

REPLACE("123456", 2, "GRM") -> "1GRM56"

but

SREPL("123456", "2", "GRM") -> "1GRM3456"
Quote:
Code:
REPLACE("123456", 2, "GRM") -> "1GRM56"

but

Code:
REPLACE("123456", "2", "GRM") -> "1GRM3456"
(03-20-2017 10:44 PM)Tyann Wrote: [ -> ]
Quote:
Code:
REPLACE("123456", 2, "GRM") -> "1GRM56"

but

Code:
REPLACE("123456", "2", "GRM") -> "1GRM3456"

Ah, I see now what you are saying. You're right.
Reference URL's