Les fonctions de chaînes de caractères sont utilisées dans les langages de programmation informatique pour manipuler une chaîne de caractères ou interroger des informations sur une chaîne de caractères (certaines font les deux).
La plupart des langages de programmation disposant d'un type de données chaîne de caractères possèdent des fonctions de manipulation de chaînes, bien qu'il existe d'autres méthodes de bas niveau, propres à chaque langage, pour manipuler directement les chaînes. Dans les langages orientés objet, ces fonctions sont souvent implémentées comme des propriétés et des méthodes d'objets chaînes. Dans les langages fonctionnels et les langages basés sur les listes, une chaîne est représentée par une liste (de codes de caractères) ; par conséquent, toutes les procédures de manipulation de listes peuvent être considérées comme des fonctions de chaînes. Cependant, ces langages peuvent également implémenter un sous-ensemble de fonctions spécifiques aux chaînes.
Pour les fonctions manipulant des chaînes de caractères, les langages orientés objet modernes, comme C# et Java, utilisent des chaînes immuables et renvoient une copie (dans une nouvelle zone mémoire dynamique allouée), tandis que d'autres, comme C, modifient la chaîne originale à moins que le programmeur ne copie les données dans une nouvelle chaîne. Voir par exemple la concaténation ci-dessous.
L'exemple le plus simple de fonction de chaîne de caractères est la length(string)fonction `length`. Cette fonction renvoie la longueur d'une chaîne littérale .
- par exemple,
length("hello world")renverrait 11.
D'autres langages peuvent posséder des fonctions de manipulation de chaînes de caractères présentant une syntaxe, des paramètres ou des résultats similaires, voire identiques. Par exemple, dans de nombreux langages, la fonction longueur est généralement représentée par `len(chaîne)` . La liste ci-dessous des fonctions courantes vise à limiter cette confusion.
La concaténation de chaînes et les expressions régulières sont traitées sur des pages séparées. Les guillemets (« … ») sont facultatifs.CharAt
| Définition | charAt(string,integer)renvoie un caractère. |
|---|---|
| Description | Renvoie le caractère à l'index spécifié dans la chaîne. |
| Équivalent | Voir la sous-chaîne de longueur 1 caractère. |
string[i]string[i]string{i}string(i)Mid(string,i,1)MID$(string,i,1)string.Chars(i)string(i:i)string.charAt(i)string.[i]string.chars().nth(i)string[i,1]String.sub (string, i)string!! i(string-ref stringi)(char stringi)(elt stringi)(get stringi)substr(string, i, 1)substr(string, i, 1)string.substr(i, 1)substr(string, i, 1)substr(string, i, 1)string.at(i)lists:nth(i, string)[string characterAtIndex:i]NSString *uniquement)string.sub(string, i, i)(string):sub(i, i)string at: istring index string iStringTake[string, {i}]string@istring (i:1)${string_param:i:1}i⌷stringComparer (résultat entier)
compare(string1,string2)renvoie un entier.| Format | Langues |
|---|---|
IF string1<string2 THEN -1 ELSE ABS (string1>string2) FI | ALGOL 68 |
cmp(string1, string2) | Python 2 |
(string1 > string2) - (string1 < string2) | Python |
strcmp(string1, string2) | C , PHP |
std.string.cmp(string1, string2) | D |
StrComp(string1, string2) | VB , Object Pascal ( Delphi ) |
string1 cmp string2 | Perl , Raku |
string1 compare: string2 | Conversation informelle ( Squeak , Pharo ) |
string1 <=> string2 | Ruby , C++ (STL, C++20 ) |
string1.compare(string2) | C++ (STL), Swift (Foundation) |
compare(string1, string2) | Rexx , Seed7 |
compare(string1, string2, pad) | Rexx |
CompareStr(string1, string2) | Pascal , Objet Pascal ( Delphi ) |
string1.compareTo(string2) | Cobra , Java |
string1.CompareTo(string2) | VB .NET , C# , F# |
(compare string1string2) | Clojure |
(string= string1string2) | Common Lisp |
(string-compare string1string2p<p=p>) | Schéma (SRFI 13) |
(string= string1string2) | ISLISP |
compare string1string2 | OCaml |
String.compare (string1, string2) | ML standard |
compare string1string2 | Haskell |
[string]::Compare(string1, string2) | Windows PowerShell |
[string1 compare:string2] | Objectif-C ( NSString *uniquement) |
LLT(string1,string2)LLE(string1,string2)LGT(string1,string2)LGE(string1,string2) | Fortran |
string1.localeCompare(string2) | JavaScript |
| |
string compare string1string2 | Tcl |
compare(string1,string2,count) | PL/I |
string1.cmp(string2) | Rouille |
Comparer (opérateur relationnel, résultat booléen)
| Définition | string1 OP string2OU renvoie une valeur booléenne.(compare string1 string2) |
|---|---|
| Description | Compare lexicographiquement deux chaînes de caractères à l'aide d'un opérateur ou d'une fonction relationnelle. Renvoie un résultat booléen. |
| Format | Langues |
|---|---|
string1 OP string2, où OPpeut être n'importe lequel des symboles =, <>, <, >, <= et >=="}},"i":0}}] | Pascal , Object Pascal ( Delphi ), OCaml , Seed7 , Standard ML , BASIC , VB , VB.NET , F# |
string1 OP string2, où OPpeut prendre n'importe quelle valeur parmi =, /=, ≠, <, >, <=, ≤ et ≥ ; également : EQ, NE, LT, LE, GE et GTALGOL 68 | |
(stringOP? string1string2), où OPpeut être n'importe lequel des opérateurs suivants : =, -ci=, <, -ci<, >, -ci>, <=, -ci<=, >= et -ci>= (les opérateurs commençant par ' ' ne sont pas sensibles à la casse)Schème | |
(stringOP string1string2), où OPpeut être n'importe lequel des opérateurs suivants : =, -ci=, <>, -ci<>, <, -ci<, >, -ci>, <=, -ci<=, >= et -ci>= (les opérateurs commençant par ' -ci ' ne sont pas sensibles à la casse)Schéma (SRFI 13) | |
(stringOP string1string2), où OPpeut être n'importe lequel des opérateurs suivants : =, -égal, /=, -différent de, <, -inférieur, >, -supérieur, <=, -inférieur, >= et -inférieur (les opérateurs verbaux ne sont pas sensibles à la casse)Common Lisp | |
(stringOP string1string2), où OPpeut être n'importe lequel des symboles =, /=, <, >, <= et >=ISLISP | |
string1 OP string2, où OPpeut être n'importe lequel des symboles =, \=, <, >, <= et >= Rexx | |
string1 OP string2, où OPpeut être n'importe lequel des symboles suivants : =, ¬=, <, >, <=, >=, ¬< et ¬> PL/I | |
string1 OP string2, où OPpeut être n'importe lequel des symboles =, /=, <, >, <= et >=Ada | |
string1 OP string2, où OPpeut être n'importe lequel des symboles ==, /=, <, >, =< et >=Erlang | |
string1 OP string2, où OPpeut être n'importe lequel des symboles ==, /=, <, >, <= et >=Haskell | |
string1 OP string2, où OPpeut être n'importe lequel des éléments suivants : eq , ne , lt , gt , le et gePerl , Raku | |
string1 OP string2, où OPpeut être n'importe lequel des symboles ==, !=, <, >, <= et >=C++ (STL), C# , D , Go , JavaScript , Python , PHP , Ruby , Rust , Swift | |
string1 OP string2, où OPpeut être n'importe lequel des opérateurs suivants : -eq, -ceq, -ne, -cne, -lt, -clt, -gt, -cgt, -le, -cle, -ge et -cge (les opérateurs commençant par ' c ' sont sensibles à la casse)Windows PowerShell | |
string1 OP string2, où OPpeut être n'importe lequel des symboles ==, ~=, <, >, <= et >=Lua | |
string1 OP string2, où OPpeut être n'importe lequel des symboles =, ~=, <, >, <= et >=Conversation | |
string1 OP string2, où OPpeut être n'importe lequel des symboles ==, /=, <, >, <= et >=; De plus : .EQ., .NE., .LT., .LE., .GT. et .GE.Fortran | |
string1 OP string2où OPpeut être n'importe lequel des symboles =, <>, <, >, <=, >= ainsi que leurs équivalents formulésCOBOL | |
string1 OP string2où OPpeut être n'importe lequel des symboles ==, <>, <, >, <= et >=Cobra | |
string1 OP string2Cette syntaxe est disponible, mais elle compare les pointeurs vers les chaînes, et non leur contenu. Utilisez la fonction Compare(entier). | C , Java |
string1.METHOD(string2)où METHODse trouve l'un des éléments suivants eq: , ne, gt, lt, ge,le | Rouille |
% Exemple en Erlang "hello" > "world" . % renvoie faux
Exemple en Common Lisp : `( string< art" "painting" ) ` renvoie `nil` ; `( string< art" "painting" ) ` renvoie une valeur non nulle
Enchaînement
concatenate(string1,string2)renvoie une chaîne de caractères.| Format | Langues |
|---|---|
string1adjacent_tostring2 | Rexx (culant, équivalent à )string1||string2 |
string1whitespacestring2 | Rexx (équivalent à )string1|| ' ' ||string2 |
string1 & string2 | Ada , FreeBASIC , Seed7 , BASIC , VB , VB.NET , COBOL (entre littéraux uniquement) |
strcat(string1, string2) | C , C++ ( uniquement) char* |
string1 . string2 | Perl , PHP |
string1 + string2 | ALGOL 68 , C++ (STL), C# , Cobra , FreeBASIC , Go , Pascal , Object Pascal ( Delphi ), Java , JavaScript , Windows PowerShell , Python , Ruby , Rust , F# , Swift , Turing , VB |
string1 ~ string2 | D , Raku |
(string-append string1string2) | Schéma , ISLISP |
(concatenate 'string string1string2) | Common Lisp |
(str string1string2) | Clojure |
string1||string2 | Rexx , SQL , PL/I |
string1 // string2 | Fortran |
string1 ++ string2 | Erlang , Haskell |
string1 ^ string2 | OCaml , Standard ML , F# |
[string1 stringByAppendingString:string2] | Objectif-C ( NSString *uniquement) |
string1 .. string2 | Lua |
string1 , string2 | Smalltalk , APL |
string1string2 | SNOBOL |
string1string2 | Frapper |
string1 <> string2 | Mathematica |
| concaténer la chaîne 1 et la chaîne 2 | Tcl |
Contient
| Définition | contains(string,substring)renvoie une valeur booléenne |
|---|---|
| Description | Indique si la chaîne contient la sous- chaîne recherchée. Cela revient à utiliser la fonction Rechercher et à vérifier qu'elle ne génère pas l'erreur mentionnée dans la troisième colonne de la section Rechercher. Cependant, certains langages proposent une méthode plus simple pour exprimer ce test. |
| En rapport | Trouver |
| Format | Langues |
|---|---|
string_in_string(string, loc int, substring) | ALGOL 68 |
ContainsStr(string, substring) | Objet Pascal ( Delphi ) |
strstr(string, substring)!= NULL | C , C++ ( char *uniquement) |
string.Contains(substring) | C# , VB.NET , Windows PowerShell , F# |
string.contains(substring) | Cobra , Java (1.5+), Raku , Rust , C++ ( C++23 ) |
string.indexOf(substring) >= 0 | JavaScript |
strpos(string, substring)!== false | PHP |
str_contains(string, substring) | PHP (8+) |
pos(string, substring) <> 0 | Graine 7 |
substring in string | Cobra , Python (2.3+) |
string.find(string, substring) ~= nil | Lua |
string.include?(substring) | Rubis |
Data.List.isInfixOf substringstring | Haskell ( GHC 6.6+) |
string includesSubstring: substring | Smalltalk ( Squeak , Pharo , Smalltalk/X ) |
String.isSubstring substringstring | ML standard |
(search substringstring) | Common Lisp |
| |
(substring? substringstring) | Clojure |
! StringFreeQ[string, substring] | Mathematica |
index(string, substring, startpos)>0 | Choisissez Basique |
strings.Contains(string, substring) | Aller |
string.find(substring)!= string::npos | C++ |
[string containsString:substring] | Objective-C ( NSString *uniquement, iOS 8+/OS X 10.10+) |
string.rangeOfString(substring)!= nil | Swift (Fondation) |
∨/substring⍷string | APL |
¢ Exemple en ALGOL 68 ¢ chaîne dans chaîne("e", loc int , "Salut mon pote"); ¢ renvoie vrai ¢ chaîne dans chaîne("z", loc int , "mot"); ¢ renvoie faux ¢
Égalité
Vérifie si deux chaînes de caractères sont égales. Voir aussi `#Compare` et `#Compare` . Effectuer des tests d'égalité via une comparaison générique avec un résultat entier est non seulement source de confusion pour le programmeur, mais s'avère souvent beaucoup plus coûteux ; c'est particulièrement vrai lors de l'utilisation de chaînes de caractères C.
| Format | Langues |
|---|---|
string1 == string2 | Python , C++ (STL), C# , Cobra , Go , JavaScript (similitude), PHP (similitude), Ruby , Rust , Erlang , Haskell , Lua , D , Mathematica , Swift |
string1 === string2 | JavaScript , PHP |
string1 == string2string1 .EQ. string2 | Fortran |
strcmp(string1, string2) == 0 | C |
(string=? string1string2) | Schème |
(string= string1string2) | Common Lisp , ISLISP |
string1 = string2 | ALGOL 68 , Ada , Object Pascal ( Delphi ), OCaml , Pascal , Rexx , Seed7 , Standard ML , BASIC , VB , VB.NET , F# , Smalltalk , PL/I , COBOL |
test string1 = string2[ string1 = string2 ] | Bourne Shell |
string1 eq string2 | Perl , Raku , Tcl |
string1.equals(string2) | Cobra , Java |
string1.Equals(string2) | C# |
string1 -eq string2[string]::Equals(string1, string2) | Windows PowerShell |
[string1 isEqualToString:string2][string1 isEqual:string2] | Objectif-C ( NSString *uniquement) |
string1 ≡ string2 | APL |
string1.eq(string2) | Rouille |
find(string,substring)renvoie un entier| Format | Langues | Si introuvable |
|---|---|---|
string in string(substring, pos, string[startpos:]) | ALGOL 68 | renvoie BOOL : TRUE ou FALSE, et la position dans REF INT pos. |
InStr(«startpos,»string,substring) | VB (les positions commencent à 1) | renvoie 0 |
INSTR$(string,substring) | BASIQUE (les positions commencent à 1) | renvoie 0 |
index(string,substring) | AWK | renvoie 0 |
index(string,substring«,startpos») | Perl 5 | renvoie −1 |
index(string,substring«,startpos»)string.index(substring,«,startpos») | Raku | renvoie Nul |
instr(«startpos,»string,substring) | FreeBASIC | renvoie 0 |
strpos(string,substring«,startpos») | PHP | renvoie FAUX |
locate(string, substring) | Ingres | renvoie la longueur de la chaîne + 1 |
strstr(string, substring) | C , C++ ( uniquement, renvoie un pointeur vers le premier caractère)char* | renvoie NULL |
std.string.indexOf(string, substring) | D | renvoie −1 |
pos(string, substring«, startpos») | Graine 7 | renvoie 0 |
strings.Index(string, substring) | Aller | renvoie −1 |
pos(substring, string) | Pascal , Objet Pascal ( Delphi ) | renvoie 0 |
pos(substring, string«,startpos») | Rexx | renvoie 0 |
string.find(substring«,startpos») | C++ (STL) | renvoie std::string::npos |
string.find(substring«,startpos«,endpos»») | Python | renvoie −1 |
string.index(substring«,startpos«,endpos»») | génère une erreur de valeur | |
string.index(substring«,startpos») | Rubis | renvoie nul |
string.indexOf(substring«,startpos») | Java , JavaScript | renvoie −1 |
string.IndexOf(substring«,startpos«, charcount»») | VB .NET , C# , Windows PowerShell , F# | renvoie −1 |
string:str(string, substring) | Erlang | renvoie 0 |
(string-contains stringsubstring) | Schéma (SRFI 13) | renvoie #f |
(search substringstring) | Common Lisp | renvoie NIL |
(string-index substringstring) | ISLISP | retoursnil |
List.findIndex (List.isPrefixOf substring) (List.tails string) | Haskell (renvoie uniquement l'index ) | ne renvoie rien |
Str.search_forward (Str.regexp_string substring) string 0 | OCaml | Lève une exception Not_found |
Substring.size (#1 (Substring.position substring (Substring.full string))) | ML standard | renvoie la longueur de la chaîne |
[string rangeOfString:substring].location | Objectif-C ( NSString *uniquement) | Renvoie NSNotFound |
string.find(string, substring)(string):find(substring) | Lua | renvoie nul |
string indexOfSubCollection: substring startingAt: startpos ifAbsent: aBlockstring findString: substring startingAt: startpos | Conversation informelle ( Squeak , Pharo ) | L'évaluation de aBlock, qui est une fermeture de bloc (ou tout objet comprenant une valeur), renvoie 0. |
startpos = INDEX(string, substring «,back» «, kind») | Fortran | Renvoie 0 si la sous-chaîne n'est pas présente dans la chaîne ; renvoie LEN(chaîne)+1 si la sous-chaîne est vide. |
POSITION(substring IN string) | SQL | renvoie 0 (les positions commencent à 1) |
index(string, substring, startpos ) | PL/I | renvoie 0 (les positions commencent à 1) |
index(string, substring, occurrence ) | Choisissez Basique | Renvoie 0 si la sous-chaîne n'est pas présente dans la chaîne ; (les positions commencent à 1) |
string.indexOf(substring«,startpos«, charcount»») | Cobra | renvoie −1 |
string first substring string startpos | Tcl | renvoie −1 |
(substring⍷string)⍳1 | APL | renvoie 1 + la dernière position dans la chaîne |
string.find(substring) | Rouille | retoursNone |
Exemples
- Common Lisp
- "Salut mon pote" ) ; renvoie 1 ( recherche "z" "mot" ) ; renvoie NIL
- C#
- .IndexOf ( "e" ) ; // renvoie 1 "Hello mate" .IndexOf ( " e" , 4 ); // renvoie 9 "word" .IndexOf ( " z" ); // renvoie -1
- Raku
- . index ( 'e' ) # renvoie 1 "Bonjour !" . index ( 'z' ) # renvoie Nil
- Schème
- "e" ) ; renvoie 1 ( string-contains "word" "z" ) ; renvoie #f
- Visual Basic
- , "e" ) ' renvoie 2 InStr ( 5 , "Hello mate" , "e" ) ' renvoie 10 InStr ( "word" , "z" ) ' renvoie 0
- Conversation
Trouver le personnage
| Définition | find_character(string,char)renvoie un entier |
|---|---|
| Description | Renvoie la position de la première occurrence du caractère `char` dans la chaîne `string` . Si le caractère est introuvable, la plupart de ces fonctions renvoient un index invalide (-1 pour les index commençant à 0, 0 pour les index commençant à 1) ou une valeur booléenne `FALSE`. Ceci peut être réalisé comme un cas particulier de `#Find` , avec une chaîne d'un seul caractère ; cependant, dans de nombreux langages, il peut être plus simple ou plus efficace de localiser directement un seul caractère. De plus, dans de nombreux langages, les caractères et les chaînes de caractères sont considérés comme des types différents, ce qui rend cette fonction utile. |
| En rapport | trouver |
| Format | Langues | Si introuvable | |||||
|---|---|---|---|---|---|---|---|
char in string(char, pos, string[startpos:]) | ALGOL 68 | renvoie BOOL : TRUE ou FALSE , et position dans REF INT pos .FreeBASIC | renvoie 0 | ||||
strchr(string,char) | C , C++ ( uniquement, renvoie un pointeur vers un caractère)char* | renvoie NULLD | renvoie −1 | ||||
string.find(char«,startpos») | C++ (STL) | renvoie std::string::nposGraine 7 | renvoie 0 | ||||
strings.IndexRune(string,char) | Aller | renvoie −1 | |||||
string.indexOf(char«,startpos») | Java , JavaScript | renvoie −1 | |||||
string.IndexOf(char«,startpos«, charcount»») | VB .NET , C# , Windows PowerShell , F# | renvoie −1 | |||||
(position charstring) | Common Lisp | renvoie NILISLISP | renvoie nulHaskell (retourne )Just index | ne renvoie rienOCaml | Lève une exception Not_found[a] | Fortran | renvoie zéro |
string indexOf: char ifAbsent: aBlockstring indexOf: charstring includes: char | Conversation | évaluer aBlockqui est un BlockClosure(ou tout objet comprenant la valeur) renvoie 0 renvoie trueoufalse | |||||
index(string, char, startpos ) | PL/I | renvoie 0 (les positions commencent à 1) | |||||
string.index(?char) | Rubis | renvoie nulPHP | renvoie fauxCobra | renvoie −1 | |||
string⍳char | APL | renvoie 1 + la dernière position dans la chaîne | |||||
string.find(substring) | Rouille | renvoie Aucun.IndexOf ( 'e' ); // renvoie 1 "word" .IndexOf ( ' z' ) // renvoie -1 ) ; renvoie 1 ( position #\z "word" ) ; renvoie NIL ^a Étant donné un ensemble de caractères, SCAN renvoie la position du premier caractère trouvé, tandis que VERIFY renvoie la position du premier caractère qui n'appartient pas à l'ensemble. FormatDéfinition | format(formatstring, items)renvoie une chaîne de caractèresDescription | Renvoie la représentation sous forme de chaîne formatée d'un ou plusieurs éléments. | |
| Format | Langues | Syntaxe de la chaîne de formatage |
|---|---|---|
associate(file, string); ALGOL 68 | ALGOL | |
Format(item, formatstring) | VB | |
sprintf(formatstring, items) | Perl , PHP , Raku , Ruby | C |
item.fmt(formatstring) | Raku | C |
io_lib:format(formatstring, items) | Erlang | |
sprintf(outputstring, formatstring, items) | C | C |
std::format(formatstring, items) | C++ ( C++20 ) | Python |
std.string.format(formatstring, items) | D | C |
Format(formatstring, items) | Objet Pascal ( Delphi ) | |
fmt.Sprintf(formatstring, items) | Aller | C |
printfformatstringitems | Unix | C |
formatstring% (items) | Python , Ruby | C |
formatstring.format(items) | Python | .FILET |
fformatstring | Python 3 | |
Printf.sprintf formatstringitems | OCaml , F# | C |
Text.Printf.printf formatstringitems | Haskell ( GHC ) | C |
formatstring printf: items | Conversation | C |
String.format(formatstring, items) | Java | C |
String.Format(formatstring, items) | VB .NET , C# , F# | .FILET |
(format formatstringitems) | Schéma (SRFI 28) | Zézayer |
(format nil formatstringitems) | Common Lisp | Zézayer |
(format formatstringitems) | Clojure | Zézayer |
formatstring -f items | Windows PowerShell | .FILET |
[NSString stringWithFormat:formatstring, items] | Objectif-C ( NSString *uniquement) | C |
String(format:formatstring, items) | Swift (Fondation) | C |
string.format(formatstring, items)(formatstring):format(items) | Lua | C |
WRITE (outputstring, formatstring) items | Fortran | Fortran |
put string(string) edit(items)(format) | PL/I | PL/I (similaire à Fortran) |
String.format(formatstring, items) | Cobra | .FILET |
format formatstring items | Tcl | C |
formatnumbers ⍕ itemsformatstring ⎕FMT items | APL | APL |
format!(formatstring, items) | Rouille | Python |
Inégalité
Vérifie si deux chaînes de caractères sont différentes. Voir aussi #Égalité .
| Format | Langues |
|---|---|
string1nestring2string1 NE string2 | ALGOL 68 – L’opérateur « ne » apparaît en caractères gras . |
string1 /= string2 | ALGOL 68 , Ada , Erlang , Fortran , Haskell |
string1 <> string2 | BASIC , VB , VB.NET , Pascal , Object Pascal ( Delphi ), OCaml , PHP , Seed7 , Standard ML , F# , COBOL , Cobra , Python 2 (obsolète) |
string1 # string2 | BASIC (certaines implémentations) |
string1 ne string2 | Perl , Raku |
(string<> string1string2) | Schéma (SRFI 13) |
(string/= string1string2) | Common Lisp |
(string/= string1string2) | ISLISP |
(not= string1string2) | Clojure |
string1!= string2 | C++ (STL), C# , Go , JavaScript (non similaire), PHP (non similaire), Python , Ruby , Rust , Swift , D , Mathematica |
string1!== string2 | JavaScript , PHP |
string1 \= string2 | Rexx |
string1 ¬= string2 | PL/I |
test string1!= string2[ string1!= string2 ] | Bourne Shell |
string1 -ne string2 | |
string1 ~= string2 | Lua , Smalltalk |
string1 ≢ string2 | APL |
string1.ne(string2) | Rouille |
index de
voir #Trouver
instr
voir #Trouver
instrrev
voir #rfind
rejoindre
| Définition | join(separator, list_of_strings)renvoie une liste de chaînes de caractères reliées par un séparateur. |
|---|---|
| Description | Concatène la liste de chaînes de caractères en une nouvelle chaîne, en insérant la chaîne de séparation entre chaque sous-chaîne. Contraire de split . |
| En rapport | sprintf |
| Format | Langues |
|---|---|
std.string.join(array_of_strings, separator) | D |
string:join(list_of_strings, separator) | Erlang |
join(separator, list_of_strings) | Perl , PHP , Raku |
implode(separator, array_of_strings) | PHP |
separator.join(sequence_of_strings) | Python , Swift 1.x |
array_of_strings.join(separator) | Ruby , JavaScript , Raku , Rust |
(string-join array_of_stringsseparator) | Schéma (SRFI 13) |
(format nil "~{~a~^separator~}" array_of_strings) | Common Lisp |
(clojure.string/join separatorlist_of_strings)(apply str (interpose separatorlist_of_strings)) | Clojure |
strings.Join(array_of_strings, separator) | Aller |
join(array_of_strings, separator) | Graine 7 |
String.concat separatorlist_of_strings | OCaml |
String.concatWith separatorlist_of_strings | ML standard |
Data.List.intercalate separatorlist_of_strings | Haskell ( GHC 6.8+) |
Join(array_of_strings, separator) | VB |
String.Join(separator, array_of_strings) | VB .NET , C# , F# |
String.join(separator, array_of_strings) | Java 8+ |
&{$OFS=$separator; "$array_of_strings"}array_of_strings -join separator | Windows PowerShell |
[array_of_strings componentsJoinedByString:separator] | Objectif-C ( NSString *uniquement) |
table.concat(table_of_strings, separator) | Lua |
| |
array_of_strings.join(separator«, final_separator») | Cobra |
sequence_of_strings.joinWithSeparator(separator) | Swift 2.x |
1↓∊separator,¨list_of_strings | APL |
dernier index de
voir #rfind
gauche
| Définition | left(string,n)renvoie une chaîne de caractères |
|---|---|
| Description | Renvoie les n premiers caractères d'une chaîne. Si n est supérieur à la longueur de la chaîne, la plupart des implémentations renvoient la chaîne entière (des exceptions existent – voir les exemples de code). Pour les encodages à longueur variable tels que UTF-8 , UTF-16 ou Shift-JIS , il peut être nécessaire de supprimer les caractères à la fin de la chaîne afin d'éviter les chaînes invalides. |
| Format | Langues |
|---|---|
| |
substr(string, 0, n) | AWK (modifie les chaînes de caractères), Perl , PHP , Raku |
LEFT$(string,n) | BASIC , VB |
left(string,n) | VB , FreeBASIC , Ingres , Pick Basic |
strncpy(string2, string, n) | bibliothèque standard C |
string.substr(0,n) | C++ (STL), Raku |
[string substringToIndex:n] | Objectif-C ( NSString *uniquement) |
| |
string[0 .. n] | D |
string:substr(string, start, length) | Erlang |
(subseq string 0 n) | Common Lisp |
string[:n] | Cobra , Go , Python |
left(string,n «,padchar») | Rexx , Erlang |
string[0, n]string[0..n - 1] | Rubis |
string[1, n] | Choisissez Basique |
string[ .. n] | Graine 7 |
string.Substring(0,n) | VB .NET , C# , Windows PowerShell , F# |
leftstr(string, n) | Pascal , Objet Pascal ( Delphi ) |
copy (string,1,n) | Turbo Pascal |
string.substring(0,n) | Java , JavaScript |
(string-take stringn) | Schéma (SRFI 13) |
take nstring | Haskell |
String.extract (string, n, NONE) | ML standard |
String.sub string 0 n | OCaml |
string.[..n] | Fa# |
string.sub(string, 1, n)(string):sub(1, n) | Lua |
string first: n | Conversation informelle ( Squeak , Pharo ) |
string(:n) | Fortran |
StringTake[string, n] | Mathematica |
string («FUNCTION» LENGTH(string) - n:n) | COBOL |
string.substring(0, n) | Cobra |
n↑string. | APL |
string[0..n]string[..n]string.get(0..n)string.get(..n) | Rouille |
len
voir #longueur
longueur
| Définition | length(string)renvoie un nombre entier |
|---|---|
| Description | Renvoie la longueur d'une chaîne (sans compter le caractère nul de fin de chaîne ni aucune autre information structurelle interne). Une chaîne vide renvoie une longueur de 0. |
| Format | Retours | Langues |
|---|---|---|
string'Length | | Ada |
UPB string | | ALGOL 68 |
echo "${#string_param}" | | Frapper |
length(string) | | Ingres , Perl 5, Pascal , Object Pascal ( Delphi ), Rexx , Seed7 , SQL , PL/I |
len(string) | | BASIC , FreeBASIC , Python , Go , Choisir Basic |
length(string), string:len(string) | | Erlang |
Len(string) | | VB , Choisissez Basique |
string.Length | Nombre d' unités de code UTF-16 | VB .NET , C# , Windows PowerShell , F# |
chars(string)string.chars | Nombre de graphèmes (NFG) | Raku |
codes(string)string.codes | Nombre de points de code Unicode | Raku |
string.size OR string.length | Nombre d'octets | Rubis |
strlen(string) | Nombre d'octets | C , PHP |
string.length() | | C++ (STL) |
string.length | | Cobra , D , JavaScript |
string.length() | Nombre d' unités de code UTF-16 | Java |
(string-length string) | | Schème |
(length string) | | Common Lisp , ISLISP |
(count string) | | Clojure |
String.length string | | OCaml |
size string | | ML standard |
length string | Nombre de points de code Unicode | Haskell |
string.length | Nombre d' unités de code UTF-16 | Objectif-C ( NSString *uniquement) |
string.characters.count | Nombre de caractères | Swift (2.x) |
count(string) | Nombre de caractères | Swift (1.2) |
countElements(string) | Nombre de caractères | Swift (1.0–1.1) |
string.len(string)(string):len()#string | | Lua |
string size | | Conversation |
LEN(string)LEN_TRIM(string) | | Fortran |
StringLength[string] | | Mathematica |
«FUNCTION» LENGTH(string)ou
| nombre de caractères et nombre d'octets, respectivement | COBOL |
string length string | une chaîne décimale indiquant le nombre de caractères | Tcl |
≢ string | APL | |
string.len() | Nombre d'octets | Rouille |
string.chars().count() | Nombre de points de code Unicode | Rouille |
Minuscule
| Définition | lowercase(string)renvoie une chaîne de caractères |
|---|---|
| Description | Renvoie la chaîne en minuscules. |
| Format | Langues |
|---|---|
LCase(string) | VB |
lcase(string) | FreeBASIC |
lc(string) | Perl , Raku |
string.lc | Raku |
tolower(char) | C |
std.string.toLower(string) | D |
transform(string.begin(), string.end(), result.begin(),::tolower) | C++ |
lowercase(string) | Objet Pascal ( Delphi ) |
strtolower(string) | PHP |
lower(string) | Graine 7 |
${string_param,,} | Frapper |
echo "string" |tr 'A-Z' 'a-z' | Unix |
string.lower() | Python |
downcase(string) | Choisissez Basique |
string.downcase | Rubis |
strings.ToLower(string) | Aller |
(string-downcase string) | Schéma (R6RS), Common Lisp |
(lower-case string) | Clojure |
String.lowercase string | OCaml |
String.map Char.toLower string | ML standard |
map Char.toLower string | Haskell |
string.toLowerCase() | Java , JavaScript |
to_lower(string) | Erlang |
string.ToLower() | VB .NET , C# , Windows PowerShell , F# |
string.lowercaseString | Objective-C ( NSString *uniquement), Swift (Foundation) |
string.lower(string)(string):lower() | Lua |
string asLowercase | Conversation |
LOWER(string) | SQL |
lowercase(string) | PL/I |
ToLowerCase[string] | Mathematica |
«FUNCTION» LOWER-CASE(string) | COBOL |
string.toLower | Cobra |
string tolower string | Tcl |
string.to_lowercase() | Rouille |
partition
| Définition | <string>.partition( separator ) renvoie la sous-chaîne avant le séparateur ; le séparateur ; puis la sous-chaîne après le séparateur. |
|---|---|
| Description | Divise la chaîne donnée en fonction du séparateur et renvoie les trois sous-chaînes qui, ensemble, constituent la chaîne originale. |
| Format | Langues | Commentaires |
|---|---|---|
string.partition(separator) | Python , Ruby (1.9+) | |
lists:partition(pred, string) | Erlang | |
split /(separator)/, string, 2 | Perl 5 | |
split separator, string, 2string.split( separator, 2 ) | Raku | Le séparateur ne doit pas nécessairement être une expression régulière. |
replace(string, find, replace)renvoie une chaîne de caractères| Format | Langues |
|---|---|
changestr(find, string, replace) | Rexx |
std.string.replace(string, find, replace) | D |
Replace(string, find, replace) | VB |
replace(string, find, replace) | Graine 7 |
change(string, find, replace) | Choisissez Basique |
string.Replace(find, replace) | C# , F# , VB .NET |
str_replace(find, replace, string) | PHP |
re:replace(string, find, replace, «{return, list}») | Erlang |
string.replace(find, replace) | Cobra , Java (1.5+), Python , Rust |
string.replaceAll(find_regex, replace) | Java |
string.gsub(find, replace) | Rubis |
string =~ s/find_regex/replace/g | Perl 5 |
string.subst(find, replace, :g) | Raku |
string.replace(find, replace, "g") string.replace(/find_regex/g, replace) | JavaScript |
echo "string" |sed 's/find_regex/replace/g' | Unix |
${string_param//find_pattern/replace} | Frapper |
string.replace(find, replace)string -replace find_regex, replace | Windows PowerShell |
Str.global_replace (Str.regexp_string find) replacestring | OCaml |
[string stringByReplacingOccurrencesOfString:find withString:replace] | Objectif-C ( NSString *uniquement) |
string.stringByReplacingOccurrencesOfString(find, withString:replace) | Swift (Fondation) |
string.gsub(string, find, replace)(string):gsub(find, replace) | Lua |
string copyReplaceAll: find with: replace | Conversation informelle ( Squeak , Pharo ) |
string map {findreplace} string | Tcl |
StringReplace[string, find -> replace] | Mathematica |
strings.Replace(string, find, replace, -1) | Aller |
INSPECT string REPLACING ALL/LEADING/FIRST find BY replace | COBOL |
find_regex ⎕R replace_regex ⊢ string | APL |
inverse
| Définition | reverse(string) |
|---|---|
| Description | Inverse l'ordre des caractères dans la chaîne. |
| Format | Langues |
|---|---|
reverse string | Perl 5, Haskell |
flip stringstring.flip | Raku |
lists:reverse(string) | Erlang |
strrev(string) | PHP |
string[::-1] | Python |
(string-reverse string) | Schéma (SRFI 13) |
(reverse string) | Common Lisp |
string.reverse | Ruby , D (modifie la chaîne) |
new StringBuilder(string).reverse().toString() | Java |
std::reverse(string.begin(), string.end()); | C++ ( std::stringmodifie uniquement la chaîne) |
StrReverse(string) | VB |
string.Reverse() | VB .NET , C# |
implode (rev (explode string)) | ML standard |
string | |
string.reverse(string)(string):reverse() | Lua |
string reverse | Conversation |
StringReverse[string] | Mathematica |
reverse(string) | PL/I |
| |
string.toCharArray.toList.reversed.join() | Cobra |
String(string.characters.reverse()) | Swift (2.x) |
String(reverse(string)) | Swift (1.2) |
string reverse string | Tcl |
⌽string | APL |
string | |
echo string| rev | Unix |
rfind
| Définition | rfind(string,substring)renvoie un entier |
|---|---|
| Description | Renvoie la position du début de la dernière occurrence de la sous-chaîne dans la chaîne . Si la sous-chaîne est introuvable, la plupart de ces routines renvoient une valeur d'index invalide – -1 lorsque les index commencent à 0, 0 lorsqu'ils commencent à 1 – ou une valeur à interpréter comme FAUX. |
| En rapport | instr |
| Format | Languages |
|---|---|
string[startpos:endpos] | ALGOL 68 (changes base index) |
string (startpos .. endpos) | Ada (changes base index) |
Mid(string, startpos, numChars) | VB |
mid(string, startpos, numChars) | FreeBASIC |
string[startpos+(⍳numChars)-~⎕IO] | APL |
MID$(string, startpos, numChars) | BASIC |
substr(string, startpos, numChars) | AWK (changes string), Perl 5,PHP |
substr(string, startpos, numChars)string.substr(startpos, numChars) | Raku |
substr(string, startpos «,numChars, padChar») | PL/I |
substr(string, startpos «,numChars, padChar») | Rexx |
string[startpos:endpos] | Cobra, Python,Go |
string[startpos, numChars] | Pick Basic |
string[startpos, numChars]string[startpos .. endpos-1]string[startpos ... endpos] | Ruby |
string[startpos .. endpos]string[startpos len numChars] | Seed7 |
string.slice(startpos«, endpos») | JavaScript |
string.substr(startpos«, numChars») | C++ (STL), JavaScript |
string.Substring(startpos, numChars) | VB .NET, C#, Windows PowerShell, F# |
string.substring(startpos«, endpos») | Java, JavaScript |
copy(string, startpos, numChars) | Object Pascal (Delphi) |
(substring stringstartposendpos) | Scheme |
(subseq stringstartposendpos) | Common Lisp |
(subseq stringstartposendpos) | ISLISP |
String.sub stringstartposnumChars | OCaml |
substring (string, startpos, numChars) | Standard ML |
string:sub_string(string, startpos, endpos)string:substr(string, startpos, numChars) | Erlang |
strncpy(result, string + startpos, numChars); | C |
string[startpos .. endpos+1] | D |
take numChars $ drop startposstring | Haskell |
[string substringWithRange:NSMakeRange(startpos, numChars)] | Objective-C (NSString * only) |
string.[startpos..endpos] | F# |
string.sub(string, startpos, endpos)(string):sub(startpos, endpos) | Lua |
string copyFrom: startpos to: endpos | Smalltalk |
string(startpos:endpos) | Fortran |
SUBSTRING(string FROM startpos «FOR numChars») | SQL |
StringTake[string, {startpos, endpos}] | Mathematica |
string (startpos:numChars) | COBOL |
${string_param:startpos:numChars} | Bash |
string range string startpos endpos | Tcl |
string[startpos..endpos]string.get(startpos..endpos) | Rust |
Uppercase
| Definition | uppercase(string) returns string |
|---|---|
| Description | Returns the string in upper case. |
| Format | Languages |
|---|---|
UCase(string) | VB |
ucase(string) | FreeBASIC |
toupper(string) | AWK (changes string) |
uc(string) | Perl, Raku |
string.uc | Raku |
toupper(char) | C (operates on one character) |
| |
std.string.toUpper(string) | D |
transform(string.begin(), string.end(), result.begin(), toupper) | C++ |
uppercase(string) | Object Pascal (Delphi) |
upcase(char) | Object Pascal (Delphi) (operates on one character) |
strtoupper(string) | PHP |
upper(string) | Seed7 |
${string_param^^} (mnemonic: ^ is pointing up) | Bash |
echo "string" |tr 'a-z' 'A-Z' | Unix |
translate(string)UPPER variablesRexx | |
string.upper() | Python |
upcase(string) | Pick Basic |
string.upcase | Ruby |
strings.ToUpper(string) | Go |
(string-upcase string) | Scheme, Common Lisp |
String.uppercase string | OCaml |
String.map Char.toUpper string | Standard ML |
map Char.toUpper string | Haskell |
string.toUpperCase() | Java, JavaScript |
string.uppercase() | Kotlin |
to_upper(string) | Erlang |
string.ToUpper() | VB .NET, C#, Windows PowerShell, F# |
string.uppercaseString | Objective-C (NSString * only), Swift (Foundation) |
string.upper(string)(string):upper() | Lua |
string asUppercase | Smalltalk |
UPPER(string) | SQL |
ToUpperCase[string] | Mathematica |
| |
string.toUpper | Cobra |
string toupper string | Tcl |
string.to_uppercase() | Rust |
trim
String.Trim([chars])string.strip();(.trim string)sequence [ predicate? ] trimCommon Lisp(string-trim string)string.trim()Trim(String)string.strip()strings.Trim(string, chars)LTRIM(RTRIM(String))strip(string [,option, char])string:strip(string [,option, char])string.stripstring.lstripstring.rstripstring.trimtrim(string)[stringObjective-C using Cocoastring withBlanksTrimmedstring withoutSpacesstring withoutSeparatorsstrip(string)string trim $stringTRIM(string)TRIM(ADJUSTL(string))TRIM(string)TRIM(string)LTrim(string)RTrim(String)String.trim stringOther languages
In languages without a built-in trim function, it is usually simple to create a custom function which accomplishes the same task.
APL
APL can use regular expressions directly:
With boost's function named simply trim the input sequence is modified in-place, and returns no result.
Another open source C++ library Qt, has several trim variants, including a standard one:
strstrip(), since 2.6.18-rc1, which trims the string "in place". Since 2.6.33-rc1, the kernel uses strim() instead of strstrip() to avoid false warnings.Haskell
A trim algorithm in Haskell:
importData.Char(isSpace)trim::String->Stringtrim=f.fwheref=reverse.dropWhileisSpace
may be interpreted as follows: f drops the preceding whitespace, and reverses the string. f is then again applied to its own output. The type signature (the second line) is optional.
J
The trim algorithm in J is a functional description:
Example:
There are, however, two functions that are commonly used to strip whitespace from the end of strings, chomp and chop:
chopremoves the last character from a string and returns it.chompremoves the trailing newline character(s) from a string if present. (What constitutes a newline is $INPUT_RECORD_SEPARATOR dependent).
In Raku, the upcoming sister language of Perl, strings have a trim method.
Example:
string command has three relevant subcommands: trim, trimright and trimleft. For each of those commands, an additional argument may be specified: a string that represents a set of characters to remove—the default is whitespace (space, tab, newline, carriage return).Example of trimming vowels:
normalize-space(string) which strips leading and trailing whitespace, in addition to replacing any whitespace sequence (including line breaks) with one space.Example:
Plus d articles de Worldlex Wiki
Revenez a l index pour explorer davantage de pages sur l histoire, la science, la culture, la geographie et la societe en francais.
Explorer l index