String
- In Turkish
- Dize
In short
A string is a data type that represents text as an ordered sequence of characters, such as a name, a sentence, a URL, or the contents of a file.
What is a string in programming?
A string is a piece of text stored as a sequence of characters, such as letters, digits, spaces, punctuation, and emoji. In most languages you create one by wrapping text in quotes, like "hello" or 'hello', and each character has a position, called an index, that usually starts at 0. Strings are among the most common data types, used for user input, messages, file paths, URLs, and data formats like JSON.
Under the hood, each character is stored as a number according to a character encoding. Modern software uses Unicode, which assigns a number called a code point to characters from almost every writing system, and encodings such as UTF-8 and UTF-16 turn those numbers into bytes. This is why string length can be surprising: in JavaScript, most emoji have a length of 2, because each one takes two UTF-16 code units.
You can picture a string as beads on a necklace, where each bead is one character and the order matters. Languages provide many built-in string operations: joining strings (concatenation), extracting part of one (a substring), searching, replacing, splitting, and changing case. Many also support string interpolation, which inserts values into text, like template literals in JavaScript or f-strings in Python.
In many languages, including JavaScript, Python, Java, and C#, strings are immutable, meaning methods like toUpperCase() return a new string instead of changing the original. A common confusion is between a string and a character: Java, C, and C# have a separate char type for a single character, while JavaScript and Python simply use a string of length one. Another is between the string '42' and the number 42, which look alike but behave differently, so '42' + 1 gives '421' in JavaScript.
Key takeaways
- A string is an ordered sequence of characters used to represent text.
- String literals are written in quotes, and characters are accessed by an index starting at 0.
- Characters are stored as numbers using Unicode encodings such as UTF-8 and UTF-16.
- In most popular languages strings are immutable, so operations return new strings.
- The string
'42'and the number42are different types.
Example
const first = "Ada";
const last = "Lovelace";
// Concatenation and a template literal (string interpolation)
const full = first + " " + last;
const greeting = `Hello, ${first}!`;
console.log(full.length); // 12
console.log(full[0]); // "A" (indexes start at 0)
console.log(full.toUpperCase()); // "ADA LOVELACE" (a new string)
console.log(full.split(" ")); // ["Ada", "Lovelace"]
console.log("42" + 1); // "421", not 43Readers ask
Why are strings immutable in many languages?
Immutable strings are safe to share between parts of a program and between threads, because no code can change them unexpectedly. They also let languages reuse identical strings in memory and use them reliably as keys in hash tables.
What is the difference between a string and a char?
A char holds exactly one character, while a string holds a sequence of zero or more characters. Java, C, and C# have a separate char type, but JavaScript and Python represent a single character as a string of length one.
What is an empty string?
An empty string, written "", is a string with zero characters. It is a real string value, which is different from null or undefined, which mean there is no value at all.
See also
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- ArrayProgramming Fundamentals, p. 3An array is an ordered collection of values stored under one name, where each item is accessed by its numeric position, called an index, usually starting at 0.
- JSONBackend & APIs, p. 25JSON is a lightweight, text-based format for storing and exchanging structured data as key-value pairs and lists, readable by both humans and machines.
- Hash TableData Structures, p. 18A hash table is a data structure that stores key-value pairs and uses a hash function to find the value for any key in constant time on average.
- Character EncodingProgramming Fundamentals, p. 8Character encoding is the set of rules that turns text into bytes and back again, so any letter can be stored and sent; today the standard is UTF-8.
Spotted a mistake or something missing on this page?Suggest an edit