A string is a collection of contiguous characters.
Strings are commonly implemented as arrays of characters. In higher platforms such as Java and .NET, strings are represented by classes, which at any time can be converted into an array of characters.
Common operations on strings are:
- length/count - the number of characters in the string
- concatenate - puts together two or more strings, in order, into a new string.
- index/character at - queries the string for a character at the specified index.
- regular expressions
Declaration[]
Strings are usually declared the same way in different programming languages, but sometimes, there might be minor differences.
Java[]
The first letter is capitalized, as in Java a string is a class.
String example="Hello, world!";
C++[]
In C++, strings are not a standard datatype, but an implementation of 'vector-like' char data structures. The come as part of the Standard Library. First letter is NOT capitalized!
#include <string> string example="Hello, world!";
Implementations[]
Null-terminated string[]
A common implementation is the null terminated string. In this setup, the string type itself is declared as an array with a fixed size, and the end of the string is marked with the null character. The string can then be read out by a while loop that terminates when the character is the null character.
Collection of characters[]
This implementation uses a strongly-typed collection of characters to represent a string. This requires determining the length of the string beforehand.
External links[]
- Algorithms on strings category on wikipedia
![]() |