"String functions" redirects here. For string functions in formal language theory, see String operations. Further information: Comparison of programming languages Comparison ofprogramming languages General comparison Assignment Basic syntax Basic instructions Comments Control flow Foreach loops While loops For loops Do-while Exception handling Enumerated types Algebraic data types Generators Anonymous functions Conditional expressions Arrays Associative arrays Scope String operations String functions Higher-order functions Filter Fold Map Type systems Dependent types List comprehension Object-oriented programming Object-oriented constructors Operators Ternary conditional operator Null coalescing operators Safe nigation operators Modulo operators Evaluation strategy List of "Hello World" programs Comparison of individuallanguages ALGOL 58's influence on ALGOL 60 ALGOL 60: Comparisons with other languages ALGOL 68: Comparisons with other languages Compatibility of C and C++ Comparison of Pascal and Borland Delphi Comparison of Object Pascal and C Comparison of Pascal and C Comparison of Ja and C++ Comparison of C# and Ja Comparison of C# and Visual Basic .NET Comparison of Visual Basic and Visual Basic .NET vte
String functions are used in computer programming languages to manipulate a string or query information about a string (some do both).
Most programming languages that he a string datatype will he some string functions although there may be other low-level ways within each language to handle strings directly. In object-oriented languages, string functions are often implemented as properties and methods of string objects. In functional and list-based languages a string is represented as a list (of character codes), therefore all list-manipulation procedures could be considered string functions. However such languages may implement a subset of explicit string-specific functions as well.
For function that manipulate strings, modern object-oriented languages, like C# and Ja he immutable strings and return a copy (in newly allocated dynamic memory), while others, like C manipulate the original string unless the programmer copies data to a new string. See for example Concatenation below.
The most basic example of a string function is the length(string) function. This function returns the length of a string literal.
e.g. length("hello world") would return 11.Other languages may he 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 len(string). The below list of common functions aims to help limit this confusion.
Common string functions (multi language reference)[edit] This section does not cite any sources. Please help improve this section by adding citations to reliable sources. Unsourced material may be challenged and removed. (July 2013) (Learn how and when to remove this message)String functions common to many languages are listed below, including the different names used. The below list of common functions aims to help programmers find the equivalent function in a language. Note, string concatenation and regular expressions are handled in separate pages. Statements in guillemets (« … ») are optional.
CharAt[edit] Definition charAt(string,integer) returns character. Description Returns character at index in the string. Equivalent See substring of length 1 character. Format Languages Base index string[i] ALGOL 68, APL, Julia, Pascal, Object Pascal (Delphi), Seed7 1 string[i] C, C++, C#, Cobra, D, FreeBASIC, Go, Python,[1] PHP, Ruby,[1] Windows PowerShell, JaScript, APL 0 string{i} PHP (deprecated in 5.3) 0 string(i) Ada ≥1 Mid(string,i,1) VB 1 MID$(string,i,1) BASIC 1 string.Chars(i) VB.NET 0 string(i:i) Fortran 1 string.charAt(i) Ja, JaScript 0 string.[i] OCaml, F# 0 string.chars().nth(i) Rust[2] 0 string[i,1] Pick Basic 1 String.sub (string, i) Standard ML 0 string !! i Haskell 0 (string-ref string i) Scheme 0 (char string i) Common Lisp 0 (elt string i) ISLISP 0 (get string i) Clojure 0 substr(string, i, 1) Perl 5[1] 0 substr(string, i, 1)string.substr(i, 1) Raku[3] 0 substr(string, i, 1) PL/I 1 string.at(i) C++ (STL) (w/ bounds checking) 0 lists:nth(i, string) Erlang 1 [string characterAtIndex:i] Objective-C (NSString * only) 0 string.sub(string, i, i)(string):sub(i, i) Lua[1] 1 string at: i Smalltalk (w/ bounds checking) 1 string index string i Tcl 0 StringTake[string, {i}] Mathematica, Wolfram Language[1] 1 string@i Eiffel 1 string (i:1) COBOL 1 ${string_param:i:1} Bash 0 i⌷string APL 0 or 1 { Example in Pascal } var MyStr: string = 'Hello, World'; MyChar: Char; begin MyChar := MyStr[2]; // 'e' # Example in ALGOL 68 # "Hello, World"[2]; // 'e' // Example in C #include // 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)' import std; using namespace std; char myStr1[] = "Hello(1), World(1)"; string myStr2 = "Hello(2), World(2)"; println("Hello(3), World(3)"); // 'Hello(3), World(3)' println("{}", myStr2[6]); // '2' println("{}", myStr1.substr(5, 3)); // '(1)' // Example in C# "Hello, World"[2]; // 'l' # Example in Perl 5 substr("Hello, World", 1, 1); # 'e' # Examples in Python "Hello, World"[2] # 'l' "Hello, World"[-3] # 'r' # Example in Raku "Hello, World".substr(1, 1); # 'e' ' Example in Visual Basic Mid("Hello, World",2,1) ' Example in Visual Basic .NET "Hello, World".Chars(2) ' "l"c " Example in Smalltalk " 'Hello, World' at: 2. "$e" //Example in Rust "Hello, World".chars().nth(2); // Some('l') Compare (integer result)[edit] Definition compare(string1,string2) returns integer. Description Compares two strings to each other. If they are equivalent, a zero is returned. Otherwise, most of these routines will return a positive or negative result corresponding to whether string1 is lexicographically greater than, or less than, respectively, than string2. The exceptions are the Scheme and Rexx routines which return the index of the first mismatch, and Smalltalk which answer a comparison code telling how the receiver sorts relative to string parameter. Format Languages IF string1string2) 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 Smalltalk (Squeak, Pharo) string1 string2 Ruby, C++ (STL, C++20)[4] string1.compare(string2) C++ (STL), Swift (Foundation) compare(string1, string2) Rexx, Seed7 CompareStr(string1, string2) Pascal, Object Pascal (Delphi) string1.compareTo(string2) Cobra, Ja string1.CompareTo(string2) VB .NET, C#, F# (compare string1 string2) Clojure (string= string1 string2) Common Lisp (string-compare string1 string2 p) Scheme (SRFI 13) (string= string1 string2) ISLISP compare string1 string2 OCaml String.compare (string1, string2) Standard ML[5] compare string1 string2 Haskell[6] [string]::Compare(string1, string2) Windows PowerShell [string1 compare:string2] Objective-C (NSString * only) LLT(string1,string2)LLE(string1,string2)LGT(string1,string2)LGE(string1,string2) Fortran[7] string1.localeCompare(string2) JaScript bytes.Compare([]byte(string1), []byte(string2)) Go string compare string1 string2 Tcl compare(string1,string2,count) PL/I[8] string1.cmp(string2) Rust[9] # Example in Perl 5 "hello" cmp "world"; # returns -1 # Example in Python cmp("hello", "world") # returns -1 # Examples in Raku "hello" cmp "world"; # returns Less "world" cmp "hello"; # returns More "hello" cmp "hello"; # returns Same /** Example in Rexx */ compare("hello", "world") /* returns index of mismatch: 1 */ ; Example in Scheme (use-modules (srfi srfi-13)) ; returns index of mismatch: 0 (string-compare "hello" "world" values values values) Compare (relational operator-based, Boolean result)[edit] Definition string1 OP string2 OR (compare string1 string2) returns Boolean. Description Lexicographically compares two strings using a relational operator or function. Boolean result returned. Format Languages string1 OP string2, where OP can be any of =, , , = Pascal, Object Pascal (Delphi), OCaml, Seed7, Standard ML, BASIC, VB, VB .NET, F# string1 OP string2, where OP can be any of =, /=, ≠, , "world". % returns false # Example in Raku "art" gt "painting"; # returns False "art" lt "painting"; # returns True # Example in Windows PowerShell "hello" -gt "world" # returns false ;; Example in Common Lisp (string> "art" "painting") ; returns nil (string replace] Mathematica strings.Replace(string, find, replace, -1) Go INSPECT string REPLACING ALL/LEADING/FIRST find BY replace COBOL find_regex ⎕R replace_regex ⊢ string APL // Examples in C# "effffff".Replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump" "blah".Replace("z", "y"); // returns "blah" // Examples in Ja "effffff".replace("f", "jump"); // returns "ejumpjumpjumpjumpjumpjump" "effffff".replaceAll("f*", "jump"); // returns "ejump" // Examples in Raku "effffff".subst("f", "jump", :g); # returns "ejumpjumpjumpjumpjumpjump" "blah".subst("z", "y", :g); # returns "blah" ' Examples in Visual Basic Replace("effffff", "f", "jump") ' returns "ejumpjumpjumpjumpjumpjump" Replace("blah", "z", "y") ' returns "blah" # Examples in Windows PowerShell "effffff" -replace "f", "jump" # returns "ejumpjumpjumpjumpjumpjump" "effffff" -replace "f*", "jump" # returns "ejump" reverse[edit] Definition reverse(string) Description Reverses the order of the characters in the string. Format Languages reverse string Perl 5, Haskell flip stringstring.flip Raku lists:reverse(string) Erlang strrev(string) PHP string[::-1] Python (string-reverse string) Scheme (SRFI 13) (reverse string) Common Lisp string.reverse Ruby, D (modifies string) new StringBuilder(string).reverse().toString() Ja std::reverse(string.begin(), string.end()); C++ (std::string only, modifies string) StrReverse(string) VB string.Reverse() VB .NET, C# implode (rev (explode string)) Standard ML string.split("").reverse().join("") JaScript string.reverse(string)(string):reverse() Lua string reverse Smalltalk StringReverse[string] Mathematica reverse(string) PL/I «FUNCTION» REVERSE(string) COBOL 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.chars().rev().collect::() Rust[42] echo string | rev Unix " Example in Smalltalk " 'hello' reversed " returns 'olleh' " # Example in Perl 5 reverse "hello" # returns "olleh" # Example in Raku "hello".flip # returns "olleh" # Example in Python "hello"[::-1] # returns "olleh" ; Example in Scheme (use-modules (srfi srfi-13)) (string-reverse "hello") ; returns "olleh" rfind[edit] Definition rfind(string,substring) returns integer Description Returns the position of the start of the last 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. Related instr Format Languages If not found InStrRev(«startpos,» string,substring) VB returns 0 instrrev(«startpos,» string,substring) FreeBASIC returns 0 rindex(string,substring«,startpos») Perl 5 returns −1 rindex(string,substring«,startpos»)string.rindex(substring«,startpos») Raku returns Nil strrpos(string,substring«,startpos») PHP returns FALSE string.rfind(substring«,startpos») C++ (STL) returns std::string::npos std.string.rfind(string, substring) D returns −1 string.rfind(substring«,startpos«, endpos»») Python returns −1 string.rindex(substring«,startpos«, endpos»») raises ValueError rpos(string, substring«,startpos») Seed7 returns 0 string.rindex(substring«,startpos») Ruby returns nil strings.LastIndex(string, substring) Go returns −1 string.lastIndexOf(substring«,startpos») Ja, JaScript returns −1 string.LastIndexOf(substring«,startpos«, charcount»») VB .NET, C#, Windows PowerShell, F# returns −1 (search substring string :from-end t) Common Lisp returns NIL [string rangeOfString:substring options:NSBackwardsSearch].location Objective-C (NSString * only) returns NSNotFound Str.search_backward (Str.regexp_string substring) string (Str.length string - 1) OCaml raises Not_found string.match(string, '.*()'..substring)string:match('.*()'..substring) Lua returns nil Ada.Strings.Unbounded.Index(Source => string, Pattern => substring, Going => Ada.Strings.Backward) Ada returns 0 string.lastIndexOf(substring«,startpos«, charcount»») Cobra returns −1 string lastIndexOfString:substring Smalltalk returns 0 string last substring string startpos Tcl returns −1 (⌽= str) { if (!isspace(*s)) { break; } *s = 0; } } void ltrim(char* str) { size_t n; n = 0; while (str[n] && isspace((unsigned char) str[n])) { n++; } memmove(str, str + n, strlen(str) - n + 1); } void trim(char* str) { rtrim(str); ltrim(str); }The open source C++ library Boost has several trim variants, including a standard one:[58]
#include trimmed = boost::algorithm::trim_copy("string");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:[59]
#include trimmed = s.trimmed();The Linux kernel also includes a strip function, 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 oid false warnings.[60]
Haskell[edit]A trim algorithm in Haskell:
import Data.Char (isSpace) trim :: String -> String trim = f . f where f = reverse . dropWhile isSpacemay be interpreted as follows: f drops the preceding whitespace, and reverses the string. f is then again applied to its own output. Note that the type signature (the second line) is optional.
J[edit]The trim algorithm in J is a functional description:
trim =. #~ [: (+./\ *. +./\.) ' '&~:That is: filter (#~) for non-space characters (' '&~:) between leading (+./\) and (*.) trailing (+./\.) spaces.
JaScript[edit]There is a built-in trim function in JaScript 1.8.1 (Firefox 3.5 and later), and the ECMAScript 5 standard. In earlier versions it can be added to the String object's prototype as follows:
String.prototype.trim = function() { return this.replace(/^\s+/g, "").replace(/\s+$/g, ""); }; Perl[edit]Perl 5 has no built-in trim function. However, the functionality is commonly achieved using regular expressions.
Example:
$string =~ s/^\s+//; # remove leading whitespace $string =~ s/\s+$//; # remove trailing whitespaceor:
$string =~ s/^\s+|\s+$//g ; # remove both leading and trailing whitespaceThese examples modify the value of the original variable $string.
Also ailable for Perl is StripLTSpace in String::Strip from CPAN.
There are, however, two functions that are commonly used to strip whitespace from the end of strings, chomp and chop:
chop removes the last character from a string and returns it. chomp removes 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 he a trim method.
Example:
$string = $string.trim; # remove leading and trailing whitespace $string .= trim; # same thing Tcl[edit]The Tcl 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:
set string onomatopoeia set trimmed [string trim $string aeiou] ;# result is nomatop set r_trimmed [string trimright $string aeiou] ;# result is onomatop set l_trimmed [string trimleft $string aeiou] ;# result is nomatopoeia XSLT[edit]XSLT includes the function normalize-space(string) which strips leading and trailing whitespace, in addition to replacing any whitespace sequence (including line breaks) with a single space.
Example:
XSLT 2.0 includes regular expressions, providing another mechanism to perform string trimming.
Another XSLT technique for trimming is to utilize the XPath 2.0 substring() function.
References[edit] ^ a b c d e the index can be negative, which then indicates the number of places before the end of the string. ^ In Rust, the str::chars method iterates over code points and the std::iter::Iterator::nth method on iterators returns the zero-indexed nth value from the iterator, or None. ^ the index can not be negative, use *-N where N indicate the number of places before the end of the string. ^ In C++, the overloaded operator method on a string returns a std::strong_ordering object (otherwise std::weak_ordering): less, equal (same as equivalent), or greater. ^ returns LESS, EQUAL, or GREATER ^ returns LT, EQ, or GT ^ returns .TRUE. or .FALSE.. These functions are based on the ASCII collating sequence. ^ a b IBM extension. ^ In Rust, the Ord::cmp method on a string returns an Ordering: Less, Equal, or Greater. ^ The original REXX used ¬= for Logical Not, ANSI Rexx uses \=, some implementations accept ~= or ^=, and non-EBCDIC implementations vary as to whether ¬ is at code point AA or AC. ^ The original REXX used ¬= for Logical Not, some implementations expect ^=, and non-EBCDIC implementations vary as to whether ¬ is at code point AA or AC. ^ a b c d e f In Rust, the operators == and != and the methods eq, ne are implemented by the PartialEq trait, and the operators , = and the methods lt, gt, le, ge are implemented by the PartialOrd trait. ^ The operators use the compiler's default collating sequence. ^ modifies string1, which must he enough space to store the result ^ In Rust, the + operator is implemented by the Add trait. ^ See the str::contains method. ^ See the std::basic_string::contains method. ^ a b startpos is IBM extension. ^ a b See the str::find method. ^ startpos is IBM extension. ^ "scan in Fortran Wiki". Fortranwiki.org. 2009-04-30. Retrieved 2013-08-18. ^ "verify in Fortran Wiki". Fortranwiki.org. 2012-05-03. Retrieved 2013-08-18. ^ formatstring must be a fixed literal at compile time for it to he the correct type. ^ See std::format, which is imported by the Rust prelude so that it can be used under the name format. ^ See the slice::join method. ^ if n is larger than the length of the string, then in Debug mode ArrayRangeException is thrown, in Release mode, the behiour is unspecified. ^ if n is larger than the length of the string, Ja will throw an IndexOutOfBoundsException ^ a b if n is larger than length of string, raises Invalid_argument ^ a b if n is larger than length of string, throw the message "StringTake::take:" ^ a b c 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 &str (string reference) can be indexed by various types of ranges, including Range (0..n), RangeFrom (n..), and RangeTo (..n) because they all implement the SliceIndex trait with str being the type being indexed. The str::get method is the non-panicking way to index. It returns None in the cases in which indexing would panic. ^ Ruby lacks Unicode support ^ See the str::len method. ^ In Rust, the str::chars method iterates over code points and the std::iter::Iterator::count method on iterators consumes the iterator and returns the total number of elements in the iterator. ^ operates on one character ^ a b The transform function exists in the std:: namespace. You must include the header file to use it. The tolower and toupper functions are in the global namespace, obtained by the header file. The std::tolower and std::toupper names are overloaded and cannot be passed to std::transform without a cast to resolve a function overloading ambiguity, e.g. std::transform(string.begin(), string.end(), result.begin(), (int (*)(int))std::tolower); ^ std::string only, result is stored in string result which is at least as long as string, and may or may not be string itself ^ a b only ASCII characters as Ruby lacks Unicode support ^ See the str::to_lowercase method. ^ See the str::replace method. ^ a b c d e The "find" string in this construct is interpreted as a regular expression. Certain characters he special meaning in regular expressions. If you want to find a string literally, you need to quote the special characters. ^ third parameter is non-standard ^ In Rust, the str::chars method iterates over code points, the std::iter::Iterator::rev method on reversible iterators (std::iter::DoubleEndedIterator) creates a reversed iterator, and the std::iter::Iterator::collect method consumes the iterator and creates a collection (which here is specified as a String with the turbofish syntax) from the iterator's elements. ^ See the str::rfind method. ^ "Annotated ES5". Es5.github.com. Archived from the original on 2013-01-28. Retrieved 2013-08-18. ^ if n is larger than length of string, then in Debug mode ArrayRangeException is thrown, and unspecified behiour in Release mode ^ See the str::split and str::rsplit methods. ^ a b c d e f g startpos can be negative, which indicates to start that number of places before the end of the string. ^ a b numChars can be negative, which indicates to end that number of places before the end of the string. ^ startpos can not be negative, use * - startpos to indicate to start that number of places before the end of the string. ^ numChars can not be negative, use * - numChars to indicate to end that number of places before the end of the string. ^ a b c d e endpos can be negative, which indicates to end that number of places before the end of the string. ^ std::string only, result is stored in string result which is at least as long as string, and may or may not be string itself ^ "uppercase - Kotlin Programming Language". Kotlin. Retrieved 9 November 2024. ^ In Rust, the str::to_uppercase method returns a newly allocated String with any lowercase characters changed to uppercase ones following the Unicode rules. ^ In Rust, the str::trim method returns a reference to the original &str. ^ "Trim – GNU Pascal priručnik". Gnu-pascal.de. Retrieved 2013-08-24. ^ "String library comparison". And.org. Retrieved 2013-08-24. ^ "Usage – 1.54.0". Boost.org. 2013-05-22. Retrieved 2013-08-24. ^ [1] Archived August 2, 2009, at the Wayback Machine ^ dankamongmen. "sprezzos-kernel-packaging/changelog at master · dankamongmen/sprezzos-kernel-packaging · GitHub". Github.com. Retrieved 2016-05-29.