ArticleslgStudy

mathematics

Comparison of programming languages (string functions)

Comparison of programming languages (string functions) is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Comparison of programming languages (string functions) rather than just read about it. In short: 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 have a string datatype will have some string functions although there may be other low-level ways within each language to handle strings directly.

Key takeaways

  • Comparison of programming languages (string functions) belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Comparison of programming languages (string functions) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Comparison of programming languages (string functions) from memory before moving on to harder problems.

Reference excerpt

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 have a string datatype will have 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 functions that manipulate strings, modern object-oriented languages, like C# and Java have 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 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 len(string). The below list of common functions aims to help limit this confusion.

Common string functions (multi language reference)

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. String concatenation and regular expressions are handled in separate pages. Statements in guillemets (« … ») are optional.

CharAt

# Example in ALGOL 68 # "Hello, World"[2]; // 'e'

Compare (integer result)

Compare (relational operator-based, Boolean result)

Concatenation

Contains

¢ Example in ALGOL 68 ¢ string in string("e", loc int, "Hello mate"); ¢ returns true ¢ string in string("z", loc int, "word"); ¢ returns false ¢

Equality Tests if two strings are equal. See also #Compare and #Compare. Doing equality checks via a generic Compare with integer result is not only confusing for the programmer but is often a significantly more expensive operation; this is especially true when using "C-strings".

Find

Examples

Common Lisp

C#

Raku

Scheme

Visual Basic

Smalltalk

Find character

^a Given a set of characters, SCAN returns the position of the first character found, while VERIFY returns the position of the first character that does not belong to the set.

Format

Inequality Tests if two strings are not equal. See also #Equality.

index see #Find

indexof see #Find

instr see #Find

instrrev see #rfind

join

lastindexof see #rfind

left

len see #length

length

locate see #Find

Lowercase

mid see #substring

partition

replace

reverse

rfind

right

rpartition

slice see #substring

split

sprintf see #Format

strip see #trim

strcmp see #Compare (integer result)

substring

Uppercase

trim

trim or strip is used to remove whitespace from the beginning, end, or both beginning and end, of a string.

Other 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:

Alternatively, a functional approach combining Boolean masks that filter away leading and trailing spaces:

Or reverse and remove leading spaces, twice:

AWK In AWK, one can use regular expressions to trim:

or:

C/C++ There is no standard trim function in C. Most of the available string libraries for C contain code which implements trimming, or functions that significantly ease an efficient implementation. The function has also often been called EatWhitespace in some non-standard C libraries. In C, programmers often combine a ltrim and rtrim to implement trim:

C++'s std::string_view has a remove_prefix(n) method, which moves the start of the view forward by n characters.

C++ also achieves string trimming using std::views::drop_while, rather than by a trim() method:

Another open source C++ library Qt, has several trim variants, including a standard method for QString:

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 avoid false warnings.

Haskell A trim algorithm in Haskell:

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:

That is: filter (#~) for non-space characters (' '&~:) between leading (+./\) and (*.) trailing (+./\.) spaces.

JavaScript There is a built-in trim function in JavaScript 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:

Perl Perl 5 has no built-in trim function. However, the functionality is commonly achieved using regular expressions. Example:

or:

These examples modify the value of the original variable $string. Also available 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 have a trim method. Example:

PL/I The PL/I trim BIF can trim characters from the left or the right. The syntax is trim(source[,left[,right]]). left specifies the characters to remove from the left while right specifies the characters to remove from the right; both default to a single space. Examples:

foo = trim(' Hi! ') /* 'Hi!' */; foo = trim('*Hi!*','*') /* 'Hi!*' */; foo = trim('*Hi!*','*','*') /* 'Hi!' */;

Rexx The Rexx strip BIF can remove leading and trailing characters. The syntax is where option may be Both (default), Leading or Trailing and char must be a single character (default ' '). Example:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Comparison of programming languages (string functions)

Start with the simplest possible case. Write down what Comparison of programming languages (string functions) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Comparison of programming languages (string functions) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Comparison of programming languages (string functions) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Comparison of programming languages (string functions)

In research
Comparison of programming languages (string functions) appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Comparison of programming languages (string functions) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Comparison of programming languages (string functions) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming language comparisons, String (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for Comparison of programming languages (string functions) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Comparison of programming languages (string functions)” →

How to study Comparison of programming languages (string functions) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Comparison of programming languages (string functions) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Comparison of programming languages (string functions) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Comparison of programming languages (string functions) in simple terms?

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 have a string datatype will have some string functions although there may be other low-level ways within each language to handle str…

Why does Comparison of programming languages (string functions) matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Comparison of programming languages (string functions)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Comparison of programming languages (string functions).

Tags

  • Programming language comparisons
  • String (computer science)

Keep exploring