Content deleted Content added
→CharAt: Add Perl 5 snippet and showcase only the requested example for Perl 6 |
m →left: {{codett}} |
||
(92 intermediate revisions by 45 users not shown) | |||
Line 1:
{{Short description|none}}
{{Redirect|String functions|string functions in formal language theory|String operations}}
{{Further|Comparison of programming languages}}
<!--
NOTE TO CONTRIBUTORS:
Thanks to all who have submitted code samples from various programming languages. When adding code samples, *please* include no extraneous characters or symbols that are not part of a programming language, such as interactive command prompts. This can needlessly confuse general readers.
All code samples should at least be able to compile without error, when copied and pasted directly
-->
{{ProgLangCompare}}
Line 24 ⟶ 19:
:e.g. <code>length("hello world")</code> would return 11.
Other languages may have string functions with similar or exactly the same syntax or parameters or outcomes. For example, in many languages the length function is usually represented as ''
==Common string functions (multi language reference)==
Line 35 ⟶ 30:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>charAt(string,integer)</code> returns character.
|-
Line 49 ⟶ 44:
|-
| <code>''string''[''i'']</code>
|[[ALGOL 68]], [[APL (programming language)|APL]], [[Julia (programming language)|Julia]], [[Pascal (programming language)|Pascal]], [[Object Pascal]] ([[Delphi (
|1
|-
| <code>''string''[''i'']</code>
|[[C (programming language)|C]], [[C++]], [[C Sharp (programming language)|C#]], [[Cobra (programming language)|Cobra]], [[D (programming language)|D]], [[FreeBASIC]], [[Go (programming language)|Go]], [[Python (programming language)|Python]],<ref name="at1"/> [[PHP]], [[Ruby (programming language)|Ruby]],<ref name="at1"/> [[Windows PowerShell]], [[JavaScript]]
|0
|-
Line 65 ⟶ 60:
|-
| <code>Mid(''string'',i,1)</code>
|[[Visual Basic (classic)|VB]]
|1
|-
Line 86 ⟶ 81:
| <code>''string''.[''i'']</code>
|[[OCaml]], [[F Sharp (programming language)|F#]]
|0
|-
| <code>''string''.chars().nth(''i'')</code>
|[[Rust (programming language)|Rust]]<ref>In Rust, the [https://doc.rust-lang.org/std/primitive.str.html#method.chars <code>str::chars</code>] method iterates over code points and the [https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.nth <code>std::iter::Iterator::nth</code>] method on iterators returns the zero-indexed nth value from the iterator, or [[option type|<code>None</code>]].</ref>
|0
|-
Line 97 ⟶ 96:
|-
| <code>''string'' !! ''i''</code>
|[[
|0
|-
Line 117 ⟶ 116:
|-
| <code>substr(''string'', ''i'', 1)</code>
|[[Perl
|0
|-
| <code>substr(''string'', ''i'', 1)</code>
|[[
|0
|-
Line 140 ⟶ 139:
|0
|-
| <code>string.sub(''string'', ''i'', ''i'')
|[[Lua (programming language)|Lua]]<ref name="at1"/>
|1
Line 163 ⟶ 162:
| [[COBOL]]
|1
|-
| <code>${''string_param'':''i'':1}</code>
| [[Bash (Unix shell)|Bash]]
|0
|-
| <code>''i''⌷''string''</code>
Line 171 ⟶ 174:
<syntaxhighlight lang="pascal">
{ Example in Pascal }
var
MyStr: string = 'Hello, World';
MyChar: Char;
Line 182 ⟶ 185:
"Hello, World"[2]; // 'e'
</pre>
<syntaxhighlight lang="c">
// Example in C
#include <stdio.h> // for printf
char MyStr[] = "Hello, World";
printf("%c", *(MyStr+1)); // 'e'
printf("%c", *(MyStr+7)); // 'W'
printf("%c", MyStr[11]); // 'd'
printf("%s", MyStr); // 'Hello, World'
printf("%s", "Hello(2), World(2)"); // 'Hello(2), World(2)'
</syntaxhighlight>
<syntaxhighlight lang="c++">
// Example in C++
#include <iostream> // for "cout"
#include <string.h> // for "string" data type
using namespace std;
char MyStr1[] = "Hello(1), World(1)";
string MyStr2 = "Hello(2), World(2)";
cout << "Hello(3), World(3)"; // 'Hello(3), World(3)'
cout << MyStr2[6]; // '2'
cout << MyStr1.substr (5, 3); // '(1)'
</syntaxhighlight>
<syntaxhighlight lang="csharp">
// Example in C#
"Hello, World"[2]; // 'l'
</syntaxhighlight>
Line 191 ⟶ 217:
# Example in Perl 5
substr("Hello, World", 1, 1); # 'e'
</syntaxhighlight>
Line 202 ⟶ 223:
"Hello, World"[2] # 'l'
"Hello, World"[-3] # 'r'
</syntaxhighlight>
<syntaxhighlight lang="perl6">
# Example in Raku
"Hello, World".substr(1, 1); # 'e'
</syntaxhighlight>
Line 217 ⟶ 243:
" Example in Smalltalk "
'Hello, World' at: 2. "$e"
</syntaxhighlight>
<syntaxhighlight lang="rust">
//Example in Rust
"Hello, World".chars().nth(2); // Some('l')
</syntaxhighlight>
<!-- endsection -->
Line 223 ⟶ 254:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>compare(string<sub>1</sub>,string<sub>2</sub>)</code> returns integer.
|-
Line 231 ⟶ 262:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>IF ''string<sub>1</sub>''<''string<sub>2</sub>'' THEN -1 ELSE ABS (''string<sub>1</sub>''>''string<sub>2</sub>'') FI</code>
Line 249 ⟶ 280:
|-
| <code>StrComp(''string<sub>1</sub>'', ''string<sub>2</sub>'')</code>
|[[Visual Basic (classic)|VB]], [[Object Pascal]] ([[Delphi (
|-
| <code>''string<sub>1</sub>'' cmp ''string<sub>2</sub>''</code>
|[[Perl
|-
| <code>''string<sub>1</sub>'' compare: ''string<sub>2</sub>''</code>
Line 258 ⟶ 289:
|-
| <code>''string<sub>1</sub>'' <=> ''string<sub>2</sub>''</code>
|[[Ruby (programming language)|Ruby]], [[C++]] (STL, [[C++20]])<ref>In C++, the overloaded [https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way%20comparison <code>operator<=></code>] method on a [https://en.cppreference.com/w/cpp/string/basic_string string] returns a [https://en.cppreference.com/w/cpp/utility/compare/strong_ordering <code>std::strong_ordering</code>] object (otherwise <code>std::weak_ordering</code>): <code>less</code>, <code>equal</code> (same as <code>equivalent</code>), or <code>greater</code>.</ref>
|-
| <code>''string<sub>1</sub>''.compare(''string<sub>2</sub>'')</code>
Line 267 ⟶ 298:
|-
| <code>CompareStr(''string<sub>1</sub>'', ''string<sub>2</sub>'')</code>
|[[Pascal (programming language)|Pascal]], [[Object Pascal]] ([[Delphi (
|-
| <code>''string<sub>1</sub>''.compareTo(''string<sub>2</sub>'')</code>
Line 291 ⟶ 322:
|-
| <code>String.compare (''string<sub>1</sub>'', ''string<sub>2</sub>'')</code>
|[[Standard ML]]
|-
| <code>compare ''string<sub>1</sub>'' ''string<sub>2</sub>''</code>
|[[
|-
| <code>[string]::Compare(''string<sub>1</sub>'', ''string<sub>2</sub>'')</code>
Line 302 ⟶ 333:
|[[Objective-C]] (<code>NSString *</code> only)
|-
| <code>LLT(''string<sub>1</sub>'',''string<sub>2</sub>'')
|[[Fortran]]
|-
| <code>''string<sub>1</sub>''.localeCompare(''string<sub>2</sub>'')</code>
Line 311 ⟶ 342:
|[[Go (programming language)|Go]]
|-
| <code>string compare
|[[Tcl]]
|-
| <code>compare(''string<sub>1</sub>'',''string<sub>2</sub>'',''count'')</code>
|[[PL/I]]<ref name="lower1">IBM extension.</ref>
|
| <code>''string<sub>1</sub>''.cmp(''string<sub>2</sub>'')</code>
| [[Rust (programming language)|Rust]]<ref>In Rust, the [https://doc.rust-lang.org/std/cmp/trait.Ord.html#tymethod.cmp <code>Ord::cmp</code>] method on a [https://doc.rust-lang.org/std/primitive.str.html#impl-Ord string] returns an [https://doc.rust-lang.org/std/cmp/enum.Ordering.html <code>Ordering</code>]: <code>Less</code>, <code>Equal</code>, or <code>Greater</code>.</ref>
|}
<syntaxhighlight lang="perl">
# Example in Perl 5
"hello" cmp "world"; # returns -1
</syntaxhighlight>
<syntaxhighlight lang="python">
# Example in Python
cmp("hello", "world") # returns -1
</syntaxhighlight>
<syntaxhighlight lang="perl6">
# Examples in Raku
"hello" cmp "world"; # returns Less
"world" cmp "hello"; # returns More
"hello" cmp "hello"; # returns Same
</syntaxhighlight>
<syntaxhighlight lang="rexx">
/** Example in Rexx */
compare("hello", "world") /* returns index of mismatch: 1 */
</syntaxhighlight>
Line 338 ⟶ 385:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>string<sub>1</sub> OP string<sub>2</sub></code> OR <code>(compare string<sub>1</sub> string<sub>2</sub>)</code> returns Boolean.
|-
Line 346 ⟶ 393:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Pascal (programming language)|Pascal]], [[Object Pascal]] ([[Delphi (
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[ALGOL 68]]
|-
| <code>(stringOP? ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>, where <code>OP</code> can be any of
|[[Scheme (programming language)|Scheme]]
|-
| <code>(stringOP ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>, where <code>OP</code> can be any of
|[[Scheme (programming language)|Scheme]] (SRFI 13)
|-
| <code>(stringOP ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>, where <code>OP</code> can be any of
|[[Common Lisp]]
|-
| <code>(stringOP ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>, where <code>OP</code> can be any of
|[[ISLISP]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Rexx]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[PL/I]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Ada (programming language)|Ada]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Erlang (programming language)|Erlang]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Perl
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[C++]] (STL), [[C Sharp (programming language)|C#]], [[D (programming language)|D]], [[Go (programming language)|Go]], [[JavaScript]], [[Python (programming language)|Python]], [[PHP]], [[Ruby (programming language)|Ruby]], [[
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Windows PowerShell]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Lua (programming language)|Lua]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Smalltalk]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code>, where <code>OP</code> can be any of
|[[Fortran]].<ref>The operators use the compiler's default collating sequence.</ref>
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code> where <code>OP</code> can be any of
| [[COBOL]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code> where <code>OP</code> can be any of
| [[Cobra (programming language)|Cobra]]
|-
| <code>''string<sub>1</sub>'' OP ''string<sub>2</sub>''</code> is available in the syntax, but means comparison of the pointers pointing to the strings, not of the string contents. Use the Compare (integer result) function.
| [[C (programming language)|C]], [[Java (programming language)|Java]]
|-
| <code>''string<sub>1</sub>''.METHOD(''string<sub>2</sub>'')</code> where <code>METHOD</code> is any of <code>eq</code>, <code>ne</code>, <code>gt</code>, <code>lt</code>, <code>ge</code>, <code>le</code>
| [[Rust (programming language)|Rust]]<ref name="Rust compare" />
|}
Line 412 ⟶ 462:
% Example in Erlang
"hello" > "world". % returns false
</syntaxhighlight>
<syntaxhighlight lang="perl6">
# Example in Raku
"art" gt "painting"; # returns False
"art" lt "painting"; # returns True
</syntaxhighlight>
Line 431 ⟶ 487:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>concatenate(string<sub>1</sub>,string<sub>2</sub>)</code> returns string.
|-
Line 440 ⟶ 496:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>''string<sub>1</sub>'' & ''string<sub>2</sub>''</code>
|[[Ada (programming language)|Ada]], [[FreeBASIC]], [[Seed7]], [[BASIC]], [[Visual Basic (classic)|VB]], [[Visual Basic .NET|VB .NET]], [[COBOL]] (between literals only)
|-
| <code> [[strcat]](''string<sub>1</sub>'', ''string<sub>2</sub>'')</code>
Line 452 ⟶ 508:
|-
| <code>''string<sub>1</sub>'' + ''string<sub>2</sub>''</code>
|[[ALGOL 68]], [[C++]] (STL), [[C Sharp (programming language)|C#]], [[Cobra (programming language)|Cobra]], [[FreeBASIC]], [[Go (programming language)|Go]], [[Pascal (programming language)|Pascal]], [[Object Pascal]] ([[Delphi (
|-
| <code>''string<sub>1</sub>'' ~ ''string<sub>2</sub>''</code>
|[[D (programming language)|D]], [[
|-
| <code>(string-append ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>
Line 473 ⟶ 529:
|-
| <code>''string<sub>1</sub>'' ++ ''string<sub>2</sub>''</code>
|[[Erlang (programming language)|Erlang]], [[
|-
| <code>''string<sub>1</sub>'' ^ ''string<sub>2</sub>''</code>
Line 491 ⟶ 547:
|-
| <code>''string<sub>1</sub>string<sub>2</sub>''</code>
|[[Bash (Unix shell)|Bash]]
|-
| <code>''string<sub>1</sub>'' <> ''string<sub>2</sub>''</code>
Line 530 ⟶ 586:
<syntaxhighlight lang="perl">
# Example in Perl 5
"abc"
"Perl "
</syntaxhighlight>
<syntaxhighlight lang="perl6">
# Example in
"abc" ~ "def"; # returns "abcdef"
"Perl " ~ 6; # returns "Perl 6"
Line 545 ⟶ 601:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>contains(''string'',''substring'')</code> returns boolean
|-
Line 562 ⟶ 618:
|-
| <code>''ContainsStr''(''string'', ''substring'')</code>
| [[Object Pascal]] ([[Delphi (
|-
| <code>strstr(''string'', ''substring'') != NULL</code>
| [[C (programming language)|C]], [[C++]] (<code>char *</code> only)
|-
| <code>''string''.Contains(''substring'')</code>
| [[C Sharp (programming language)|C#]], [[Visual Basic .NET|VB .NET]], [[Windows PowerShell]], [[F Sharp (programming language)|F#]]
|-
| <code>''string''.contains(''substring'')</code>
| [[Cobra (programming language)|Cobra]], [[Java (programming language)|Java]] (1.5+), [[
|-
| <code>''string''.indexOf(''substring'') >= 0</code>
Line 578 ⟶ 634:
| <code>strpos(''string'', ''substring'') !== false</code>
|[[PHP]]
|-
| <code>str_contains(''string'', ''substring'')</code>
|[[PHP]] (8+)
|-
| <code>pos(''string'', ''substring'') <> 0</code>
Line 592 ⟶ 651:
|-
| <code>Data.List.isInfixOf ''substring'' ''string''</code>
|[[
|-
| <code>''string'' includesSubstring: ''substring''</code>
Line 644 ⟶ 703:
</syntaxhighlight>
<syntaxhighlight lang="python">
# Example in Python
"e" in "Hello mate" # returns true
"z" in "word" # returns false
</syntaxhighlight>
<syntaxhighlight lang="perl6">
# Example in Raku
"Good morning!".contains('z') # returns False
"¡Buenos días!".contains('í'); # returns True
</syntaxhighlight>
Line 668 ⟶ 727:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>''string<sub>1</sub>'' == ''string<sub>2</sub>''</code>
|[[Python (programming language)|Python]], [[C++]] (STL), [[C Sharp (programming language)|C#]], [[Cobra (programming language)|Cobra]], [[Go (programming language)|Go]], [[JavaScript]] (similarity), [[PHP]] (similarity), [[Ruby (programming language)|Ruby]], [[
|-
| <code>''string<sub>1</sub>'' === ''string<sub>2</sub>''</code>
|[[JavaScript]], [[PHP]]
|-
| <code>''string<sub>1</sub>'' == ''string<sub>2</sub>''
|[[Fortran]]
|-
Line 689 ⟶ 748:
|-
| <code>''string<sub>1</sub>'' = ''string<sub>2</sub>''</code>
|[[ALGOL 68]], [[Ada (programming language)|Ada]], [[Object Pascal]] ([[Delphi (
|-
| <code>test ''string<sub>1</sub>'' = ''string<sub>2</sub>''
|[[Bourne Shell]]
|-
| <code>''string<sub>1</sub>'' eq ''string<sub>2</sub>''</code>
|[[Perl
|-
| <code>''string<sub>1</sub>''.equals(''string<sub>2</sub>'')</code>
Line 703 ⟶ 762:
|[[C Sharp (programming language)|C#]]
|-
| <code>''string<sub>1</sub>'' -eq ''string<sub>2</sub>''
|[[Windows PowerShell]]
|-
| <code>[''string<sub>1</sub>'' isEqualToString:''string<sub>2</sub>'']
|[[Objective-C]] (<code>NSString *</code> only)
|-
| <code>''string<sub>1</sub>'' ≡ ''string<sub>2</sub>''</code>
|[[APL (programming language)|APL]]
|-
| <code>''string<sub>1</sub>''.eq(''string<sub>2</sub>'')</code>
|[[Rust (programming language)|Rust]]<ref name="Rust compare" />
|}
Line 730 ⟶ 792:
<syntaxhighlight lang="perl6">
# Examples in
'hello' eq 'world' # returns False
'hello' eq 'hello' # returns True
Line 751 ⟶ 813:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>find(''string'',''substring'')</code> returns integer
|-
Line 757 ⟶ 819:
| Returns the position of the start of the first occurrence of ''substring'' in ''string''. If the ''substring'' is not found most of these routines return an invalid index value – -1 where indexes are 0-based, 0 where they are 1-based – or some value to be interpreted as Boolean FALSE.
|-
| instrrev
|}
Line 769 ⟶ 831:
|-
| <code>InStr(«''startpos'',»''string'',''substring'')</code>
|[[Visual Basic (classic)|VB]] (positions start at 1)
|returns 0
|-
Line 781 ⟶ 843:
|-
| <code>index(''string'',''substring''«,''startpos''»)</code>
|[[Perl
|returns −1
|-
| <code>index(''string'',''substring''«,''startpos''»)</code>
|[[Raku (programming language)|Raku]]
|returns Nil
|-
Line 817 ⟶ 879:
|-
| <code>pos(''substring'', ''string'')</code>
|[[Pascal (programming language)|Pascal]], [[Object Pascal]] ([[Delphi (
|returns 0
|-
Line 864 ⟶ 926:
|-
| <code>List.findIndex (List.isPrefixOf ''substring'') (List.tails ''string'')</code>
|[[
|returns Nothing
|-
Line 879 ⟶ 941:
|returns NSNotFound
|-
| <code>string.find(''string'', ''substring'')
|[[Lua (programming language)|Lua]]
|returns nil
|-
| <code>''string'' indexOfSubCollection: ''substring'' startingAt: ''startpos'' ifAbsent: ''aBlock''
|[[Smalltalk]] ([[Squeak]], [[Pharo]])
|evaluate aBlock which is a block closure (or any object understanding value)
|-
| <code>startpos = INDEX(''string'', ''substring'' «,''back''» «, ''kind''»)</code>
Line 914 ⟶ 976:
|[[APL (programming language)|APL]]
|returns 1 + the last position in ''string''
|-
| <code>''string''.find(''substring'')</code>
|[[Rust (programming language)|Rust]]<ref name="Rust find">See the [https://doc.rust-lang.org/stable/std/primitive.str.html#method.find <code>str::find</code>] method.</ref>
|returns [[Option type|<code>None</code>]]
|}
'''Examples'''
*: <syntaxhighlight lang="lisp">
(search "e" "Hello mate") ; returns 1
(search "z" "word") ; returns NIL
</syntaxhighlight>
* C#
*: <syntaxhighlight lang="csharp">
"Hello mate".IndexOf("e"); // returns 1
"Hello mate".IndexOf("e", 4); // returns 9
"word".IndexOf("z"); // returns -1
</syntaxhighlight>
* Raku
*: <syntaxhighlight lang="
"Hello, there!".index('e') # returns 1
"Hello, there!".index('z') # returns Nil
</syntaxhighlight>
* Scheme
*: <syntaxhighlight lang="scheme">
(use-modules (srfi srfi-13))
(string-contains "Hello mate" "e") ; returns 1
(string-contains "word" "z") ; returns #f
</syntaxhighlight>
* Visual Basic
*: <syntaxhighlight lang="vbnet">
' Examples in
InStr("Hello mate", "e") ' returns 2
InStr(5, "Hello mate", "e") ' returns 10
InStr("word", "z") ' returns 0
</syntaxhighlight>
* Smalltalk
*: <syntaxhighlight lang="smalltalk">
'Hello mate' indexOfSubCollection:'ate' "returns 8"
</syntaxhighlight>
*: <syntaxhighlight lang="smalltalk">
'Hello mate' indexOfSubCollection:'late' "returns 0"
</syntaxhighlight>
*: <syntaxhighlight lang="smalltalk">
I'Hello mate'
indexOfSubCollection:'late'
ifAbsent:[ 99 ] "returns 99"
</syntaxhighlight>
*: <syntaxhighlight lang="smalltalk">
'Hello mate'
indexOfSubCollection:'late'
ifAbsent:[ self error ] "raises an exception"
Line 967 ⟶ 1,036:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>find_character(''string'',''char'')</code> returns integer
|-
Line 982 ⟶ 1,051:
| <code>char in string(char, pos, ''string[startpos:]'')</code>
|[[ALGOL 68]]
|returns {{mono|BOOL}}: {{mono|TRUE}} or {{mono|FALSE}}, and position in {{mono|REF INT pos}}.
|-
| <code>instr(''string'', any ''char''«,''startpos''»)</code> (char, can contain more them one char, in which case the position of the first appearance of any of them is returned.)
Line 990 ⟶ 1,059:
| <code>strchr(''string'',''char'')</code>
|[[C (programming language)|C]], [[C++]] (<code>[[character (computing)|char]] [[Pointer (computer programming)|*]]</code> only, returns pointer to character)
|returns {{mono|NULL}}
|-
| <code>std.string.find(''string'', ''dchar'')</code>
Line 998 ⟶ 1,067:
| <code>''string''.find(''char''«,''startpos''»)</code>
|[[C++]] (STL)
|returns {{mono|std::string::npos}}
|-
| <code>pos(''string'', ''char''«, ''startpos''»)</code>
Line 1,018 ⟶ 1,087:
| <code>(position ''char'' ''string'')</code>
|[[Common Lisp]]
|returns {{mono|NIL}}
|-
| <code>(char-index ''char'' ''string'')</code>
|[[ISLISP]]
|returns
|-
| <code>List.elemIndex ''char'' ''string''</code>
|[[
|returns {{mono|Nothing}}
|-
| <code>String.index ''string'' ''char''</code>
|[[OCaml]]
|raises {{mono|Not_found}}
|-
| <code>position = SCAN (''string'', ''set'' «, ''back''» «, ''kind''»)
|[[Fortran]]
|returns zero
|-
| <code>''string'' indexOf: ''char'' ifAbsent: aBlock
|[[Smalltalk]]
|evaluate <code>aBlock</code> which is a <code>BlockClosure</code> (or any object understanding value)
|-
| <code>index(''string'', ''char'', ''startpos'' )</code>
Line 1,046 ⟶ 1,115:
|<code>''string''.index(?''char'')</code>
|[[Ruby (programming language)|Ruby]]
|returns {{mono|nil}}
|-
|<code>
|[[PHP]]
|returns
|-
| <code>''string''.indexOf(''char''«,''startpos''«, ''charcount''»»)</code>
Line 1,059 ⟶ 1,128:
|[[APL (programming language)|APL]]
|returns 1 + the last position in ''string''
|-
| <code>''string''.find(''substring'')</code>
|[[Rust (programming language)|Rust]]<ref name="Rust find" />
|returns [[Option type|{{mono|None}}]]
|}
Line 1,073 ⟶ 1,146:
</syntaxhighlight>
<!-- endsection -->
{{note|Fortran find|a}} Given a set of characters, SCAN returns the position of the first character found,<ref>{{cite web|url=http://fortranwiki.org/fortran/show/scan |title=scan in Fortran Wiki |publisher=Fortranwiki.org |date=2009-04-30 |
===Format===
Line 1,080 ⟶ 1,153:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>format(''formatstring'', ''items'')</code> returns string
|-
Line 1,088 ⟶ 1,161:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format
! Languages ! Format string syntax |-
| <code>associate(''file'', ''string''); [[putf]](''file'', ''$formatstring$'', ''items'')</code>
Line 1,095 ⟶ 1,170:
|-
| <code>Format(''item'', ''formatstring'')</code>
|[[Visual Basic (classic)|VB]]
| <code></code>
|-
| <code>[[sprintf]](''formatstring'', ''items'')</code>
|[[Perl
|[[C (programming language)|C]]
|-
| <code>item.fmt(''formatstring'')</code>
|[[Raku (programming language)|Raku]]
|[[C (programming language)|C]]
|-
| <code>io_lib:format(''formatstring'', ''items'')</code>
Line 1,112 ⟶ 1,187:
| <code>[[sprintf]](''outputstring'', ''formatstring'', ''items'')</code>
|[[C (programming language)|C]]
|[[C (programming language)|C]]
|-
| <code>std::format(''formatstring'', ''items'')</code>
| [[C++]] ([[C++20]])
|Python
|-
| <code>std.string.format(''formatstring'', ''items'')</code>
|[[D (programming language)|D]]
|[[C (programming language)|C]]
|-
| <code>Format(''formatstring'', ''items'')</code>
|[[Object Pascal]] ([[Delphi (
| <code></code>
|-
| <code>fmt.Sprintf(''formatstring'', ''items'')</code>
|[[Go (programming language)|Go]]
|[[C (programming language)|C]]
|-
| <code>[[printf (Unix)|printf]]
|[[
|[[C (programming language)|C]]
|-
| <code>''formatstring'' % (''items'')</code>
|[[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]]
|[[C (programming language)|C]]
|-
| <code>''formatstring''.format(''items'')</code>
|[[Python scripting language|Python]]
|[[.NET]]
|-
|<code>f''formatstring''</code>
|[[Python 3]]
|-
| <code>Printf.sprintf ''formatstring''</code><ref>''<code>formatstring</code>'' must be a fixed literal at compile time for it to have the correct type.</ref><code> ''items''</code>
| [[OCaml]], [[F Sharp (programming language)|F#]]
|[[C (programming language)|C]]
|-
| <code>Text.Printf.printf ''formatstring'' ''items''</code>
|[[
|[[C (programming language)|C]]
|-
| <code>''formatstring'' printf: ''items''</code>
|[[Smalltalk]]
|[[C (programming language)|C]]
|-
| <code>String.format(''formatstring'', ''items'')</code>
|[[Java (programming language)|Java]]
|[[C (programming language)|C]]
|-
| <code>String.Format(''formatstring'', ''items'')</code>
|[[Visual Basic .NET|VB .NET]], [[C Sharp (programming language)|C#]], [[F Sharp (programming language)|F#]]
|[[.NET]]
|-
| <code>(format ''formatstring'' ''items'')</code>
Line 1,172 ⟶ 1,254:
| <code>''formatstring'' -f ''items''</code>
|[[Windows PowerShell]]
|[[.NET]]
|-
| <code>[NSString stringWithFormat:''formatstring'', ''items'']</code>
|[[Objective-C]] (<code>NSString *</code> only)
|[[C (programming language)|C]]
|-
| <code>String(format:''formatstring'', ''items'')</code>
|[[Swift (programming language)|Swift]] (Foundation)
|[[C (programming language)|C]]
|-
| <code>string.format(''formatstring'', ''items'')
|[[Lua (programming language)|Lua]]
|[[C (programming language)|C]]
|-
| <code>WRITE (''outputstring'', ''formatstring'') ''items''</code>
Line 1,196 ⟶ 1,278:
| <code>String.format(''formatstring'', ''items'')</code>
|[[Cobra (programming language)|Cobra]]
|[[.NET]]
|-
| <code>format ''formatstring items''</code>
|[[Tcl]]
|[[C (programming language)|C]]
|-
| <code>''formatnumbers'' ⍕ ''items''</code>
|[[APL (programming language)|APL]]
|APL
|-
| <code>format!(''formatstring'', ''items'')</code>
| [[Rust (programming language)|Rust]]<ref>See [https://doc.rust-lang.org/stable/std/macro.format.html <code>std::format</code>], which is imported by the Rust [https://doc.rust-lang.org/stable/std/prelude/ prelude] so that it can be used under the name <code>format</code>.</ref>
| Python
|}
Line 1,223 ⟶ 1,309:
<syntaxhighlight lang="perl6">
# Examples in
sprintf "My %s costs \$%.2f", "pen", 19.99; # returns "My pen costs $19.99"
1.fmt("%04d"); # returns "0001"
Line 1,232 ⟶ 1,318:
"My %s costs $%.2f" % ("pen", 19.99); # returns "My pen costs $19.99"
"My {0} costs ${1:.2f}".format("pen", 19.99); # returns "My pen costs $19.99"
</syntaxhighlight>
<syntaxhighlight lang="python3">
#Example in Python 3.6+
pen = "pen"
f"My {pen} costs {19.99}" #returns "My pen costs 19.99"
</syntaxhighlight>
Line 1,239 ⟶ 1,330:
</syntaxhighlight>
<syntaxhighlight lang="c">
/* example in PL/I */
put string(some_string) edit('My ', 'pen', ' costs', 19.99)(a,a,a,p'$$$V.99')
/* returns "My pen costs $19.99" */
</syntaxhighlight>
<!-- endsection -->
Line 1,251 ⟶ 1,342:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>''string<sub>1</sub>'' '''ne''' ''string<sub>2</sub>''
|[[ALGOL 68]] – note: the operator "'''ne'''" is literally in '''bold''' type-font.
|-
| <code>''string<sub>1</sub>'' /= ''string<sub>2</sub>''</code>
|[[ALGOL 68]], [[Ada (programming language)|Ada]], [[Erlang (programming language)|Erlang]], [[Fortran]], [[
|-
| <code>''string<sub>1</sub>'' <> ''string<sub>2</sub>''</code>
|[[BASIC]], [[Visual Basic (classic)|VB]], [[Visual Basic .NET|VB .NET]], [[Pascal (programming language)|Pascal]], [[Object Pascal]] ([[Delphi (
|-
| <code>''string<sub>1</sub>'' # ''string<sub>2</sub>''</code>
Line 1,266 ⟶ 1,357:
|-
| <code>''string<sub>1</sub>'' ne ''string<sub>2</sub>''</code>
|[[Perl
|-
| <code>(string<> ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>
Line 1,272 ⟶ 1,363:
|-
| <code>(string/= ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>
|[[Common Lisp]]
|-
| <code>(string/= ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>
|[[ISLISP]]
|-
| <code>(not= ''string<sub>1</sub>'' ''string<sub>2</sub>'')</code>
|[[Clojure]]
|-
| <code>''string<sub>1</sub>'' != ''string<sub>2</sub>''</code>
|[[C++]] (STL), [[C Sharp (programming language)|C#]], [[Go (programming language)|Go]], [[JavaScript]] (not similar), [[PHP]] (not similar), [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[Rust (programming language)|Rust]],<ref name="Rust compare" /> [[Swift (programming language)|Swift]], [[D (programming language)|D]], [[Mathematica]]
|-
| <code>''string<sub>1</sub>'' !== ''string<sub>2</sub>''</code>
|[[JavaScript]], [[PHP]]
|-
| <code>''string<sub>1</sub>'' \= ''string<sub>2</sub>''</code>
|[[Rexx]]
|-
| <code>''string<sub>1</sub>'' ¬= ''string<sub>2</sub>''</code>
|[[PL/I]]
|-
| <code>test ''string<sub>1</sub>'' != ''string<sub>2</sub>''
|[[Bourne Shell]]
|-
| <code>''string<sub>1</sub>'' -ne ''string<sub>2</sub>''
|[[Windows PowerShell]]
|-
Line 1,303 ⟶ 1,394:
| <code>''string<sub>1</sub>'' ≢ ''string<sub>2</sub>''</code>
|[[APL (programming language)|APL]]
|-
| <code>''string<sub>1</sub>''.ne(''string<sub>2</sub>'')</code>
| [[Rust (programming language)|Rust]]<ref name="Rust compare" />
|}
Line 1,326 ⟶ 1,420:
<syntaxhighlight lang="perl6">
# Example in
'hello' ne 'world' # returns True
</syntaxhighlight>
Line 1,351 ⟶ 1,445:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code> join(''separator'', ''list_of_strings'')</code> returns a list of strings joined with a separator
|-
Line 1,362 ⟶ 1,456:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>std.string.join(''array_of_strings'', ''separator'')</code>
Line 1,371 ⟶ 1,465:
|-
| <code>join(''separator'', ''list_of_strings'')</code>
|[[Perl
|-
| <code>implode(''separator'', ''array_of_strings'')</code>
Line 1,380 ⟶ 1,474:
|-
| <code>''array_of_strings''.join(''separator'')</code>
|[[Ruby (programming language)|Ruby]], [[JavaScript]], [[
|-
| <code>(string-join ''array_of_strings'' ''separator'')</code>
Line 1,388 ⟶ 1,482:
|[[Common Lisp]]
|-
| <code>(clojure.string/join ''separator'' ''list_of_strings'')</code><br><code>(apply str (interpose ''separator'' ''list_of_strings''))</code>
|[[Clojure]]
|-
Line 1,405 ⟶ 1,498:
|-
| <code>Data.List.intercalate ''separator'' ''list_of_strings''</code>
|[[
|-
| <code>Join(''array_of_strings'', ''separator'')</code>
|[[Visual Basic (classic)|VB]]
|-
| <code>String.Join(''separator'', ''array_of_strings'')</code>
Line 1,416 ⟶ 1,509:
|[[Java (programming language)|Java]] 8+
|-
| <code>&{$OFS=''$separator''; "''$array_of_strings''"}
|[[Windows PowerShell]]
|-
Line 1,423 ⟶ 1,516:
|-
| <code>table.concat(''table_of_strings'', ''separator'')</code>
|[[Lua (programming language)|Lua]]
|-
| <code><nowiki>{|String streamContents: [ :stream |</nowiki> ''collectionOfAnything'' asStringOn: stream delimiter: ''separator'' <nowiki>]</nowiki>
|[[Smalltalk]] ([[Squeak]], [[Pharo]])
|-
Line 1,455 ⟶ 1,547:
<syntaxhighlight lang="perl6">
# Example in
<a b c>.join('-'); # 'a-b-c'
</syntaxhighlight>
Line 1,482 ⟶ 1,574:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>left(''string'',''n'')</code> returns string
|-
Line 1,490 ⟶ 1,582:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
|<code>{{codett|string (string'First .. string'First +|ada}} ''n'' - 1)</code>
|[[Ada (programming language)|Ada]]
|-
| <code>substr(''string'', 0, ''n'')</code>
|[[AWK]] (changes string), [[Perl
|-
| <code>LEFT$(''string'',''n'')</code>
|[[BASIC]], [[Visual Basic (classic)|VB]]
|-
| <code>left(''string'',''n'')</code>
||[[Visual Basic (classic)|VB]], [[FreeBASIC]], [[Ingres (database)|Ingres]], [[Pick Basic]]
|-
| <code>strncpy(''string2'', ''string'', ''n'')</code>
Line 1,508 ⟶ 1,600:
|-
| <code>''string''.substr(0,''n'')</code>
|[[C++]] (STL), [[
|-
| <code>[''string'' substringToIndex:''n'']</code>
|[[Objective-C]] (<code>NSString *</code> only)
|-
| <code>{{codett|(apply str (take|clojure}} ''n'' ''string''))</code>
|[[Clojure]]
|-
Line 1,531 ⟶ 1,623:
|[[Rexx]], [[Erlang (programming language)|Erlang]]
|-
| <code>''string''[0, ''n'']
|[[Ruby (programming language)|Ruby]]
|-
Line 1,544 ⟶ 1,636:
|-
| <code>leftstr(''string'', ''n'')</code>
|[[Pascal (programming language)|Pascal]], [[Object Pascal]] ([[Delphi (
|-
|<code>''copy'' (''string'',1,''n'')</code>
Line 1,550 ⟶ 1,642:
|-
| <code>''string''.substring(0,''n'')</code>
|[[Java (programming language)|Java]],<ref>if n is larger than the length of the string, Java will throw an IndexOutOfBoundsException</ref> [[JavaScript]]
|-
| <code>(string-take ''string'' ''n'')</code>
Line 1,556 ⟶ 1,648:
|-
| <code>take ''n'' ''string''</code>
|[[
|-
| <code>String.extract (''string'', ''n'', NONE)</code>
Line 1,567 ⟶ 1,659:
|[[F Sharp (programming language)|F#]]
|-
| <code>string.sub(''string'', 1, ''n'')
|[[Lua (programming language)|Lua]]
|-
Line 1,587 ⟶ 1,679:
|<code>''n''↑''string''.</code>
|[[APL (programming language)|APL]]
|-
| <code>''string''[0..n]</code><br><code>''string''[..n]</code><br><code>''string''.get(0..n)</code><br><code>''string''.get(..n)</code>
| [[Rust (programming language)|Rust]]<ref name="Rust indexing">In Rust, strings are indexed in terms of byte offsets and there is a runtime panic if the index is out of bounds or if it would result in invalid [[UTF-8]]. A <code>&str</code> (string reference) can be [https://doc.rust-lang.org/stable/std/primitive.str.html#impl-Index%3CI%3E indexed] by various types of ranges, including [https://doc.rust-lang.org/stable/std/slice/trait.SliceIndex.html#impl-SliceIndex%3Cstr%3E <code>Range</code>] (<code>0..n</code>), [https://doc.rust-lang.org/stable/std/slice/trait.SliceIndex.html#impl-SliceIndex%3Cstr%3E-1 <code>RangeFrom</code>] (<code>n..</code>), and [https://doc.rust-lang.org/stable/std/slice/trait.SliceIndex.html#impl-SliceIndex%3Cstr%3E-4 <code>RangeTo</code>] (<code>..n</code>) because they all implement the [https://doc.rust-lang.org/stable/std/slice/trait.SliceIndex.html <code>SliceIndex</code>] trait with <code>str</code> being the type being indexed.
The [https://doc.rust-lang.org/stable/std/primitive.str.html#method.get <code>str::get</code>] method is the non-panicking way to index. It returns [[option type|<code>None</code>]] in the cases in which indexing would panic.</ref>
|}
<syntaxhighlight lang="perl6">
# Example in
"Hello, there!".substr(0, 6); # returns "Hello,"
</syntaxhighlight>
Line 1,604 ⟶ 1,701:
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-take "abcde", 3) ; returns "abc"
(string-take "abcde", 8) ; error
</syntaxhighlight>
<syntaxhighlight lang="
' Examples in Visual Basic
Left("sandroguidi", 3) ' returns "san"
Left("sandroguidi", 100) ' returns "sandroguidi"
</syntaxhighlight>
<!-- endsection -->
Line 1,630 ⟶ 1,722:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>length(''string'')</code> returns an integer number
|-
Line 1,638 ⟶ 1,730:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Returns !! Languages
|-
|<code>string'Length</code>
Line 1,647 ⟶ 1,739:
| <code></code>
|[[ALGOL 68]]
|-
|<code>echo "${#''string_param''}"</code>
| <code></code>
|[[Bash (Unix shell)|Bash]]
|-
| <code>length(''string'')</code>
| <code></code>
|[[Ingres (database)|Ingres]], [[Perl
|-
| <code>
| <code></code>
|[[BASIC]], [[FreeBASIC]], [[Python (programming language)|Python]], [[Go (programming language)|Go]], [[Pick Basic]]
Line 1,660 ⟶ 1,756:
|[[Erlang (programming language)|Erlang]]
|-
| <code>
| <code></code>
|[[Visual Basic (classic)|VB]], [[Pick Basic]]
|-
| <code>''string''.Length</code>
|Number of
|[[Visual Basic .NET|VB .NET]], [[C Sharp (programming language)|C#]], [[Windows PowerShell]], [[F Sharp (programming language)|F#]]
|-
| <code>chars(''string'')</code>
|Number of graphemes (NFG)
|[[Raku (programming language)|Raku]]
|-
| <code>codes(''string'')</code>
|Number of Unicode code points
|[[Raku (programming language)|Raku]]
|-
| <code>''string''.size OR ''string''.length</code>
Line 1,693 ⟶ 1,789:
|-
| <code>''string''.length()</code>
|Number of
|[[Java (programming language)|Java]]
|-
Line 1,717 ⟶ 1,813:
|-
| <code>length ''string''</code>
|Number of Unicode
|[[
|-
| <code>''string''.length</code>
| Number of
|[[Objective-C]] (<code>NSString *</code> only)
|-
Line 1,736 ⟶ 1,832:
|[[Swift (programming language)|Swift]] (1.0–1.1)
|-
| <code>string.len(''string'')
| <code></code>
|[[Lua (programming language)|Lua]]
Line 1,744 ⟶ 1,840:
|[[Smalltalk]]
|-
| <code>LEN(''string'')
| <code></code>
|[[Fortran]]
Line 1,751 ⟶ 1,847:
| <code></code>
|[[Mathematica]]
|-
|<code>«FUNCTION» LENGTH(''string'')</code> or
Line 1,767 ⟶ 1,858:
|-
|<code>≢ ''string''</code>
|
|[[APL (programming language)|APL]]
|-
| <code>''string''.len()</code>
| Number of bytes
| [[Rust (programming language)|Rust]]<ref>See the [https://doc.rust-lang.org/stable/std/primitive.str.html#method.len <code>str::len</code>] method.</ref>
|-
| <code>''string''.chars().count()</code>
| Number of Unicode code points
| [[Rust (programming language)|Rust]]<ref>In Rust, the [https://doc.rust-lang.org/std/primitive.str.html#method.chars <code>str::chars</code>] method iterates over code points and the [https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.count <code>std::iter::Iterator::count</code>] method on iterators consumes the iterator and returns the total number of elements in the iterator.</ref>
|}
Line 1,790 ⟶ 1,889:
<syntaxhighlight lang="perl6">
# Examples in
"🏳️🌈".chars; chars "🏳️🌈"; # both return 1
"🏳️🌈".codes; codes "🏳️🌈"; # both return 4
Line 1,826 ⟶ 1,925:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>lowercase(''string'')</code> returns string
|-
! Description
| Returns the string in lower case.
|}
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>LCase(''string'')
|[[Visual Basic (classic)|VB]]
|-
| <code>lcase(''string'')
|[[FreeBASIC]]
|-
| <code>lc(''string'')</code>
|[[Perl
|-
| <code>''string''.lc</code>
|[[Raku (programming language)|Raku]]
|-
|<code>tolower(''char'')</code>
|[[C (programming language)|C]]<ref>operates on one character</ref>
|-
| <code>std.string.toLower(''string'')
|[[D (programming language)|D]]
|-
Line 1,858 ⟶ 1,957:
|-
| <code>lowercase(''string'')</code>
|[[Object Pascal]] ([[Delphi (
|-
| <code>strtolower(''string'')</code>
Line 1,865 ⟶ 1,964:
| <code>lower(''string'')</code>
|[[Seed7]]
|-
|<code>${''string_param'',,}</code>
|[[Bash (Unix shell)|Bash]]
|-
|<code>[[echo (command)|echo]] "string" <nowiki>|</nowiki> [[tr (program)|tr]] 'A-Z' 'a-z'</code>
Line 1,894 ⟶ 1,996:
|-
| <code>map Char.toLower ''string''</code>
|[[
|-
| <code>''string''.toLowerCase()</code>
Line 1,908 ⟶ 2,010:
|[[Objective-C]] (<code>NSString *</code> only), [[Swift (programming language)|Swift]] (Foundation)
|-
| <code>string.lower(''string'')
|[[Lua (programming language)|Lua]]
|-
Line 1,929 ⟶ 2,031:
|[[Cobra (programming language)|Cobra]]
|-
| <code>string tolower ''string''</code>
|[[Tcl]]
|-
| <code>''string''.to_lowercase()</code>
| [[Rust (programming language)|Rust]]<ref>See the [https://doc.rust-lang.org/stable/std/primitive.str.html#method.to_lowercase <code>str::to_lowercase</code>] method.</ref>
|}
<syntaxhighlight lang="csharp">
// Example in C#
"Wiki means fast?".ToLower(); // "wiki means fast?"
</syntaxhighlight>
Line 1,952 ⟶ 2,057:
int i;
for (i = 0; i < sizeof(string) - 1; ++i) {
/* transform characters in place, one by one */
string[i] = tolower(string[i]);
}
Line 1,961 ⟶ 2,066:
<syntaxhighlight lang="perl6">
# Example in
"Wiki means fast?".lc; # "wiki means fast?"
</syntaxhighlight>
Line 1,975 ⟶ 2,080:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <string>.partition(''separator'') returns the sub-string before the separator; the separator; then the sub-string after the separator.
|-
! Description
| Splits the given string by the separator and returns the three substrings that together make the original.
|}
{| class="wikitable sortable"
Line 1,994 ⟶ 2,099:
|-
| <code>split /(''separator'')/, ''string'', 2</code>
|[[Perl
|
|-
| <code>split ''separator'', ''string'', 2</code>
|[[Raku (programming language)|Raku]]
|Separator does not have to be a regular expression
|}
Line 2,009 ⟶ 2,114:
<syntaxhighlight lang="perl">
# Examples in Perl 5 /
split /(spam)/, 'Spam eggs spam spam and ham' ,2; # ('Spam eggs ', 'spam', ' spam and ham');
split /(X)/, 'Spam eggs spam spam and ham' ,2; # ('Spam eggs spam spam and ham');
Line 2,019 ⟶ 2,124:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>replace(''string'', ''find'', ''replace'')</code> returns string
|-
Line 2,027 ⟶ 2,132:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>changestr(''find'', ''string'', ''replace'')</code>
Line 2,036 ⟶ 2,141:
|-
| <code>Replace(''string'', ''find'', ''replace'')</code>
|[[Visual Basic (classic)|VB]]
|-
| <code>replace(''string'', ''find'', ''replace'')</code>
Line 2,045 ⟶ 2,150:
|-
| <code>''string''.Replace(''find'', ''replace'')</code>
|
|-
| <code>str_replace(''find'', ''replace'', ''string'')</code>
Line 2,054 ⟶ 2,159:
|-
| <code>''string''.replace(''find'', ''replace'')</code>
|[[Cobra (programming language)|Cobra]], [[Java (programming language)|Java]] (1.5+), [[Python (programming language)|Python]], [[
|-
| <code>''string''.replaceAll(''find_regex'', ''replace'')<ref name="regex" /></code>
Line 2,063 ⟶ 2,168:
|-
| <code>''string'' =~ s/''find_regex''/''replace''/g<ref name="regex">The "find" string in this construct is interpreted as a [[regular expression]]. Certain characters have special meaning in regular expressions. If you want to find a string literally, you need to quote the special characters.</ref></code>
|[[Perl
|-
| <code>''string''.subst(''find'', ''replace'', :g)</code>
|[[Raku (programming language)|Raku]]
|-
| <code>''string''.replace(''find'', ''replace'', "g") <ref>third parameter is non-standard</ref>
|[[JavaScript]]
|-
Line 2,074 ⟶ 2,179:
|[[Unix]]
|-
| <code>${''
|[[Bash (Unix shell)|Bash]]
|-
| <code>''string''.replace(''find'', ''replace'')</code><br><code>''string'' -replace ''find_regex'', ''replace''<ref name="regex" /></code>
|[[Windows PowerShell]]
|-
Line 2,086 ⟶ 2,194:
|[[Swift (programming language)|Swift]] (Foundation)
|-
| <code>string.gsub(''string'', ''find'', ''replace'')
|[[Lua (programming language)|Lua]]
|-
Line 2,121 ⟶ 2,229:
<syntaxhighlight lang="perl6">
// Examples in
"effffff".subst("f", "jump", :g); # returns "ejumpjumpjumpjumpjumpjump"
"blah".subst("z", "y", :g); # returns "blah"
Line 2,142 ⟶ 2,250:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>reverse(''string'')</code>
|-
Line 2,150 ⟶ 2,258:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>reverse ''string''</code>
|[[Perl]] 5
|-
| <code>flip ''string''</code>
|[[Raku (programming language)|Raku]]<!-- reverses graphemes -->
|-
| <code>lists:reverse(''string'')</code>
Line 2,183 ⟶ 2,291:
|-
| <code>StrReverse(''string'')</code>
|[[Visual Basic (classic)|VB]]
|-
| <code>''string''.Reverse
|[[Visual Basic .NET|VB .NET]], [[C Sharp (programming language)|C#]]
|-
Line 2,194 ⟶ 2,302:
|[[JavaScript]]
|-
| <code>string.reverse(''string'')
|[[Lua (programming language)|Lua]]
|-
Line 2,209 ⟶ 2,317:
|[[COBOL]]
|-
|<code>''string''.toCharArray.toList.reversed.join(
|[[Cobra (programming language)|Cobra]]
|-
Line 2,223 ⟶ 2,331:
| <code>⌽''string''</code>
|[[APL (programming language)|APL]]
|-
| <code>''string''.chars().rev().collect::<String>()</code>
| [[Rust (programming language)|Rust]]<ref>In Rust, the [https://doc.rust-lang.org/std/primitive.str.html#method.chars <code>str::chars</code>] method iterates over code points, the [https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.rev <code>std::iter::Iterator::rev</code>] method on reversible iterators ([https://doc.rust-lang.org/stable/std/iter/trait.DoubleEndedIterator.html <code>std::iter::DoubleEndedIterator</code>]) creates a reversed iterator, and the [https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.collect <code>std::iter::Iterator::collect</code>] method consumes the iterator and creates a collection (which here is specified as a [https://doc.rust-lang.org/stable/std/string/struct.String.html <code>String</code>] with the [[turbofish]] syntax) from the iterator's elements.</ref>
|-
| <code>echo ''string'' <nowiki>|</nowiki> rev</code>
| [[Unix]]
|}
Line 2,236 ⟶ 2,350:
<syntaxhighlight lang="perl6">
# Example in
"hello".flip # returns "olleh"
</syntaxhighlight>
Line 2,256 ⟶ 2,370:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>rfind(''string'',''substring'')</code> returns integer
|-
Line 2,270 ⟶ 2,384:
|-
| <code>InStrRev(«''startpos'',» ''string'',''substring'')</code>
|[[Visual Basic (classic)|VB]]
|returns 0
|-
Line 2,278 ⟶ 2,392:
|-
| <code>rindex(''string'',''substring''«,''startpos''»)</code>
|[[Perl
|returns −1
|-
| <code>rindex(''string'',''substring''«,''startpos''»)</code>
|[[Raku (programming language)|Raku]]
|returns {{mono|Nil}}
|-
| <code>strrpos(''string'',''substring''«,''startpos''»)</code>
|[[PHP]]
|returns {{mono|FALSE}}
|-
| <code>''string''.rfind(''substring''«,''startpos''»)</code>
|[[C++]] (STL)
|returns {{mono|std::string::npos}}
|-
| <code>std.string.rfind(''string'', ''substring'')</code>
Line 2,302 ⟶ 2,416:
|-
|<code>''string''.rindex(''substring''«,''startpos''«, ''endpos''»»)</code>
|raises {{mono|ValueError}}
|-
|<code>rpos(''string'', ''substring''«,''startpos''»)</code>
Line 2,310 ⟶ 2,424:
|<code>''string''.rindex(''substring''«,''startpos''»)</code>
|[[Ruby (programming language)|Ruby]]
|returns {{mono|nil}}
|-
| <code>strings.LastIndex(''string'', ''substring'')</code>
Line 2,326 ⟶ 2,440:
| <code>(search ''substring'' ''string'' :from-end t)</code>
|[[Common Lisp]]
|returns {{mono|NIL}}
|-
| <code>[''string'' rangeOfString:''substring'' options:NSBackwardsSearch].___location</code>
|[[Objective-C]] (<code>NSString *</code> only)
|returns {{mono|NSNotFound}}
|-
| <code>Str.search_backward (Str.regexp_string ''substring'') ''string'' (Str.length ''string'' - 1)</code>
|[[OCaml]]
|raises {{mono|Not_found}}
|-
| <code>string.match(''string'', '.*()'..''substring'')
|[[Lua (programming language)|Lua]]
|returns {{mono|nil}}
|-
| <code>Ada.Strings.Unbounded.Index(Source => ''string'', Pattern => ''substring'',
|[[Ada (programming language)|Ada]]
|returns 0
Line 2,359 ⟶ 2,473:
|[[APL (programming language)|APL]]
|returns −1
|-
| <code>''string''.rfind(''substring'')</code>
| [[Rust (programming language)|Rust]]<ref>See the [https://doc.rust-lang.org/stable/std/primitive.str.html#method.rfind <code>str::rfind</code>] method.</ref>
| returns [[Option type|{{mono|None}}]]
|}
Line 2,382 ⟶ 2,500:
<syntaxhighlight lang="perl6">
# Examples in
"Hello mate".rindex("e"); # returns 9
"Hello mate".rindex("e", 4); # returns 1
Line 2,400 ⟶ 2,518:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>right(''string'',''n'')</code> returns string
|-
Line 2,408 ⟶ 2,526:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
|<code>string (string'Last - ''n'' + 1 .. string'Last)</code>
Line 2,414 ⟶ 2,532:
|-
| <code>Right(''string'',''n'')</code>
|[[Visual Basic (classic)|VB]]
|-
| <code>RIGHT$(''string'',''n'')</code>
Line 2,435 ⟶ 2,553:
|-
| <code>''string''.slice(-''n'')</code>
|[[JavaScript]]<ref>{{cite web |url=https://es5.github.com/#x15.5.4.13 |title=Annotated ES5 |publisher=Es5.github.com |access-date=2013-08-18 |
|-
| <code>right(''string'',''n'' «,''padchar''»)</code>
Line 2,441 ⟶ 2,559:
|-
| <code>substr(''string'',-''n'')</code>
|[[Perl
|-
| <code>substr(''string'',*-''n'')</code>
|[[Raku (programming language)|Raku]]
|-
| <code>''string''[-''n'':]</code>
|[[Cobra (programming language)|Cobra]], [[Python (programming language)|Python]]
|-
| <code>${''string_param'': -''n''}</code> (note the space after the colon)
|[[Bash (Unix shell)|Bash]]
|-
| <code>''string''[''n'']</code>
Line 2,456 ⟶ 2,577:
|-
| <code>''string''[-''n''..-1]</code>
|[[Ruby (programming language)|Ruby]]
|-
| <code>''string''[$-''n'' .. $]</code>
Line 2,464 ⟶ 2,585:
|[[OCaml]]<ref name="ReferenceA"/>
|-
| <code>string.sub(''string'', -''n'')
|[[Lua (programming language)|Lua]]
|-
Line 2,478 ⟶ 2,599:
|<code>''¯n''↑''string''.</code>
|[[APL (programming language)|APL]]
|-
| <code>''string''[n..]</code><br><code>''string''.get(n..)</code>
| [[Rust (programming language)|Rust]]<ref name="Rust indexing" />
|}
Line 2,487 ⟶ 2,611:
<syntaxhighlight lang="perl6">
# Examples in
"abcde".substr(*-3); # returns "cde"
"abcde".substr(*-8); # 'out of range' error
Line 2,502 ⟶ 2,626:
; Examples in Scheme
(use-modules (srfi srfi-13))
(string-take-right "abcde", 3) ; returns "cde"
(string-take-right "abcde", 8) ; error
</syntaxhighlight>
Line 2,508 ⟶ 2,632:
<syntaxhighlight lang="vbnet">
' Examples in Visual Basic
Right("sandroguidi", 3) ' returns "idi"
Right("sandroguidi", 100) ' returns "sandroguidi"
</syntaxhighlight>
<!-- endsection -->
Line 2,518 ⟶ 2,641:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <string>.rpartition(''separator'') Searches for the separator from right-to-left within the string then returns the sub-string before the separator; the separator; then the sub-string after the separator.
|-
! Description
| Splits the given string by the right-most separator and returns the three substrings that together make the original.
|}
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>''string''.rpartition(''separator'')</code>
Line 2,547 ⟶ 2,670:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <string>.split(''separator''[, ''limit'']) splits a string on separator, optionally only up to a limited number of substrings
|-
Line 2,555 ⟶ 2,678:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>split(/''separator''/, ''string''«, ''limit''»)</code>
|[[Perl
|-
| <code>split(''separator'', ''string''«, ''limit''»)</code>
|[[Raku (programming language)|Raku]]
|-
| <code>explode(''separator'', ''string''«, ''limit''»)</code>
Line 2,575 ⟶ 2,698:
|[[Erlang (programming language)|Erlang]]
|-
| <code>strings.Split(''string'', ''separator'')<
|[[Go (programming language)|Go]]
|-
Line 2,582 ⟶ 2,705:
|-
| <code>Split(''string'', ''sepchars''«, ''limit''»)</code>
|[[Visual Basic (classic)|VB]]
|-
| <code>''string''.Split(''sepchars''«, ''limit''«, ''options''»»)</code>
Line 2,614 ⟶ 2,737:
|[[Tcl]]
|-
| <code>(''separator''≠''string'')⊂''string''</code>
|[[APL (programming language)|APL]]
|-
| <code>''string''.split(''separator'')</code>
<code>''string''.split(''limit'', ''separator'')</code>
| [[Rust (programming language)|Rust]]<ref>See the [https://doc.rust-lang.org/stable/std/primitive.str.html#method.split <code>str::split</code>] and [https://doc.rust-lang.org/stable/std/primitive.str.html#method.rsplit <code>str::rsplit</code>] methods.</ref>
|}
Line 2,657 ⟶ 2,784:
<syntaxhighlight lang="perl6">
# Examples in
'Spam eggs spam spam and ham'.split(/spam/); # (Spam eggs and ham)
split(/X/, 'Spam eggs spam spam and ham'); # (Spam eggs spam spam and ham)
Line 2,682 ⟶ 2,809:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>substring(''string'', ''startpos'', ''endpos'')</code> returns string
|-
! Description
| Returns a substring of ''string'' between starting at ''startpos'' and ''endpos'', or starting at ''startpos'' of length ''numChars''. The resulting string is truncated if there are fewer than ''numChars'' characters beyond the starting point. ''endpos'' represents the index after the last character in the substring. Note that for variable-length encodings such as [[UTF-8]], [[UTF-16]] or [[Shift-JIS]], it can be necessary to remove string positions at the end, in order to avoid invalid strings.
Line 2,690 ⟶ 2,817:
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
|<code>string[''startpos'':''endpos'']</code>
Line 2,699 ⟶ 2,826:
|-
| <code>Mid(''string'', ''startpos'', ''numChars'')</code>
|[[Visual Basic (classic)|VB]]
|-
| <code>mid(''string'', ''startpos'', ''numChars'')</code>
Line 2,711 ⟶ 2,838:
|-
| <code>substr(''string'', ''startpos'', ''numChars'')</code>
|[[AWK]] (changes string), [[Perl
|-
| <code>substr(''string'', ''startpos'', ''numChars'')</code>
|[[
|-
| <code>substr(''string'', ''startpos'' «,''numChars'', ''padChar''»)</code>
Line 2,725 ⟶ 2,852:
|[[Pick Basic]]
|-
| <code>''string''[''startpos'', ''numChars'']
|[[Ruby (programming language)|Ruby]]<ref name="substr2"/><ref name="substr3"/>
|-
| <code>''string''[''startpos'' .. ''endpos'']
|[[Seed7]]
|-
Line 2,744 ⟶ 2,871:
|-
| <code>copy(''string'', ''startpos'', ''numChars'')</code>
|[[Object Pascal]] ([[Delphi (
|-
| <code>(substring ''string'' ''startpos'' ''endpos'')</code>
Line 2,761 ⟶ 2,888:
|[[Standard ML]]
|-
| <code>string:sub_string(''string'', ''startpos'', ''endpos'')<
|[[Erlang (programming language)|Erlang]]
|-
Line 2,767 ⟶ 2,894:
|[[C (programming language)|C]]
|-
| <code>''string''[''startpos'' .. ''endpos''+1]</code>
|[[D (programming language)|D]]
|-
| <code>take ''numChars'' $ drop ''startpos'' ''string''</code>
|[[
|-
| <code>[''string'' substringWithRange:NSMakeRange(''startpos'', ''numChars'')]</code>
Line 2,779 ⟶ 2,906:
|[[F Sharp (programming language)|F#]]
|-
| <code>string.sub(''string'', ''startpos'', ''endpos'')
|[[Lua (programming language)|Lua]]<ref name="substr2"/><ref name="substr3"/>
|-
Line 2,794 ⟶ 2,921:
|[[Mathematica]]<ref name="substr2"/><ref name="substr3"/>
|-
| <code>''string'' (''startpos'':''numChars'')</code>
| [[COBOL]]
|-
| <code>${''string_param'':''startpos'':''numChars''}</code>
| [[Bash (Unix shell)|Bash]]
|-
|string range ''string startpos endpos''
|[[Tcl]]
|-
| <code>''string''[''startpos''..''endpos'']</code><br><code>''string''.get(''startpos''..''endpos'')</code>
| [[Rust (programming language)|Rust]]<ref name="Rust indexing" />
|}
Line 2,821 ⟶ 2,952:
string:substr("abc", 2). % returns "bc"
</syntaxhighlight>
<syntaxhighlight lang="perl">
Line 2,830 ⟶ 2,960:
<syntaxhighlight lang="perl6">
# Examples in
"abc".substr(1, 1); # returns "b"
"abc".substr(1); # returns "bc"
Line 2,855 ⟶ 2,985:
{| class="wikitable"
|- style="background:#fffeed;"
! Definition
| <code>uppercase(''string'')</code> returns string
|-
! Description
| Returns the string in upper case.
|}
{| class="wikitable sortable"
|- style="text-align:left;"
! Format !! Languages
|-
| <code>UCase(''string'')
|[[Visual Basic (classic)|VB]]
|-
| <code>ucase(''string'')
|[[FreeBASIC]]
|-
| <code>toupper(''string'')
|[[AWK]] (changes string)
|-
| <code>uc(''string'')</code>
|[[Perl
|-
| <code>''string''.uc</code>
|[[Raku (programming language)|Raku]]
|-
|<code>toupper(''char'')</code>
|[[C (programming language)|C]] (operates on one character)
|-
|<code>{{codett|2=c|1=for(size_t i = 0, len = strlen(}}''string''); i< len; i++) ''string''[i] = toupper(''string''[i]);</code><br
|[[C (programming language)|C]] (string / char array)
|-
Line 2,893 ⟶ 3,023:
|-
| <code>uppercase(''string'')</code>
|[[Object Pascal]] ([[Delphi (
|-
|<code>upcase(''char'')</code>
|[[Object Pascal]] ([[Delphi (
|-
| <code>strtoupper(''string'')</code>
Line 2,904 ⟶ 3,034:
|[[Seed7]]
|-
|<code>${''string_param''^^}</code> (mnemonic: ^ is pointing up)
|[[Bash (Unix shell)|Bash]]
|-
|<code>[[echo (command)|echo]] "string" <nowiki>|</nowiki> [[tr (program)|tr]] 'a-z' 'A-Z'</code>
|[[Unix]]
|-
| <code>translate(''string'')</code>
|[[Rexx]]
|-
Line 2,919 ⟶ 3,050:
|-
| <code>''string''.upcase</code>
|[[Ruby (programming language)|Ruby]]
|-
| <code>strings.ToUpper(''string'')</code>
Line 2,934 ⟶ 3,065:
|-
| <code>map Char.toUpper ''string''</code>
|[[
|-
| <code>''string''.toUpperCase()</code>
|[[Java (programming language)|Java]], [[JavaScript]]
|-
| <code>''string''.uppercase()</code>
|[[Kotlin (programming language)|Kotlin]]<ref>{{cite web |title=uppercase - Kotlin Programming Language |url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/uppercase.html |website=Kotlin |access-date=9 November 2024 |language=en}}</ref>
|-
| <code>to_upper(''string'')</code>
Line 2,948 ⟶ 3,082:
|[[Objective-C]] (<code>NSString *</code> only), [[Swift (programming language)|Swift]] (Foundation)
|-
| <code>string.upper(''string'')
|[[Lua (programming language)|Lua]]
|-
Line 2,966 ⟶ 3,100:
|[[Cobra (programming language)|Cobra]]
|-
| <code>string toupper ''string''</code>
| [[Tcl]]
|-
| <code>''string''.to_uppercase()</code>
| [[Rust (programming language)|Rust]]<ref>In Rust, the [https://doc.rust-lang.org/stable/std/primitive.str.html#method.to_uppercase <code>str::to_uppercase</code>] method returns a newly allocated [https://doc.rust-lang.org/stable/std/string/struct.String.html <code>String</code>] with any lowercase characters changed to uppercase ones following the Unicode rules.</ref>
|}
<syntaxhighlight lang="csharp">
// Example in C#
"Wiki means fast?".ToUpper(); // "WIKI MEANS FAST?"
</syntaxhighlight>
<syntaxhighlight lang="perl">
# Example in Perl 5
uc("Wiki means fast?"); # "WIKI MEANS FAST?"
</syntaxhighlight>
<syntaxhighlight lang="perl6">
# Example in
uc("Wiki means fast?"); # "WIKI MEANS FAST?"
"Wiki means fast?".uc; # "WIKI MEANS FAST?"
</syntaxhighlight>
Line 3,007 ⟶ 3,144:
<syntaxhighlight lang="vbnet">
' Example in Visual Basic
UCase("Wiki means fast?") ' "WIKI MEANS FAST?"
</syntaxhighlight>
<!-- endsection -->
Line 3,017 ⟶ 3,154:
{| class="wikitable"
|- style="text-align:left;"
! Example usage !! Languages
|-
| <
|[[C Sharp (programming language)|C#]], [[VB.NET]], [[Windows PowerShell]]
|-
| <
|[[D (programming language)|D]]
|-
| <
|[[Clojure]]
|-
|<
|[[Factor (programming language)|Factor]]
|-
| <
|[[Common Lisp]]
|-
| <
|[[Scheme (programming language)|Scheme]]
|-
| <
|[[Java (programming language)|Java]], [[JavaScript]] (1.8.1+, Firefox 3.5+), [[Rust (programming language)|Rust]]<ref>In Rust, the [https://doc.rust-lang.org/stable/std/primitive.str.html#method.trim <code>str::trim</code>] method returns a reference to the original <code>&str</code>.</ref>
|-
| <
|[[Pascal (programming language)|Pascal]],<ref>{{cite web|url=http://gnu-pascal.de/gpc-hr/Trim.html |title=Trim – GNU Pascal priručnik |publisher=Gnu-pascal.de |access-date
|-
| <
|[[Python (programming language)|Python]]
|-
| <
|[[Go (programming language)|Go]]
|-
| <
|[[Oracle Corporation|Oracle]] [[SQL]], [[T-SQL]]
|-
| <
|[[REXX (programming language)|REXX]]
|-
| <
|[[Erlang (programming language)|Erlang]]
|-
| <
|[[Ruby (programming language)|Ruby]]
|-
| <
|[[Raku (programming language)|Raku]]
|-
| <
|[[PHP]], [[
|-
| <
|[[Objective-C]] using [[Cocoa (API)|Cocoa]]
|-
| <
|[[Smalltalk]] ([[Squeak]], [[Pharo]])<br>[[Smalltalk]]
|-
|<
|[[SAS
|-
|<
|[[Tcl]]
|-
| <
|[[Fortran]]
|-
| <
|[[SQL]]
|-
| <
|[[ColdFusion]]
|-
| <
|[[OCaml]] 4+
|}
Line 3,098 ⟶ 3,232:
In languages without a built-in trim function, it is usually simple to create a custom function which accomplishes the same task.
====APL====
[[APL (programming language)|APL]] can use regular expressions directly:
<syntaxhighlight lang="apl">
Trim←'^ +| +$'⎕R''
</syntaxhighlight>
Alternatively, a functional approach combining Boolean masks that filter away leading and trailing spaces:
<syntaxhighlight lang="apl">
Trim←{⍵/⍨(∨\∧∘⌽∨\∘⌽)' '≠⍵}
</syntaxhighlight>
Or reverse and remove leading spaces, twice:
<syntaxhighlight lang="apl">
Trim←{(∨\' '≠⍵)/⍵}∘⌽⍣2
</syntaxhighlight>
====AWK====
Line 3,117 ⟶ 3,265:
====C/C++====
There is no standard trim function in C or C++. Most of the available string libraries<ref>{{cite web|url=http://www.and.org/vstr/comparison |title=String library comparison |publisher=And.org |access-date
In C, programmers often combine a ltrim and rtrim to implement trim:
Line 3,139 ⟶ 3,287:
size_t n;
n = 0;
while (str[n] != '\0' && isspace((unsigned char) str[n])) {
n++;
}
Line 3,152 ⟶ 3,300:
</syntaxhighlight>
The [[open-source software|open source]] C++ library [[Boost library|Boost]] has several trim variants, including a standard one:<ref>{{cite web|url=http://www.boost.org/doc/html/string_algo/usage.html#id2742817 |title=Usage – 1.54.0 |publisher=Boost.org |date=2013-05-22 |
<syntaxhighlight lang="cpp">
Line 3,159 ⟶ 3,307:
</syntaxhighlight>
Another [[open-source software|open source]] C++ library [[Qt (
<syntaxhighlight lang="cpp">
Line 3,168 ⟶ 3,316:
</syntaxhighlight>
The [[Linux kernel]] also includes a strip function, <code>strstrip()</code>, since 2.6.18-rc1, which trims the string "in place". Since 2.6.33-rc1, the kernel uses <code>strim()</code> instead of <code>strstrip()</code> to avoid false warnings.<ref>{{cite web|author=dankamongmen |url=https://github.com/dankamongmen/sprezzos-kernel-packaging/blob/master/changelog |title=sprezzos-kernel-packaging/changelog at master · dankamongmen/sprezzos-kernel-packaging · GitHub |publisher=Github.com |access-date
====Haskell====
A trim algorithm in [[
<syntaxhighlight lang="haskell">
Line 3,217 ⟶ 3,365:
Also available for Perl is '''StripLTSpace''' in <code>String::Strip</code> from [[CPAN]].
There are, however, two functions that are commonly used to strip whitespace from the end of strings, <code>chomp</code> and <code>chop</code>:
* <code>[http://perldoc.perl.org/functions/chop.html chop]</code> removes the last character from a string and returns it.
* <code>[http://perldoc.perl.org/functions/chomp.html chomp]</code> removes the trailing newline character(s) from a string if present. (What constitutes a newline is [http://perldoc.perl.org/perlvar.html $INPUT_RECORD_SEPARATOR] dependent).
In [[
Example:
<syntaxhighlight lang="
$string = $string.trim; # remove leading and trailing whitespace
$string .= trim; # same thing
Line 3,265 ⟶ 3,413:
<ref name="substr6">''<code>numChars</code>'' can '''not''' be negative, use ''* - numChars'' to indicate to end that number of places before the end of the string.</ref>
}}
[[Category:Programming language comparisons|*String functions]]
[[Category:String (computer science)|Programming language comparison]]
[[Category:Articles with example Python (programming language) code]]
|