Strings in Programming
What is a String?
A string is a sequence of characters, usually used to represent text such as words or sentences.
Strings are one of the most common data types in programming, and are enclosed in either single quotes 'Hello'
or double quotes "Hello"
, depending on the language.
name = "Jenny"
print(name)
const name = "Jenny";
console.log(name);
String name = "Jenny";
System.out.println(name);
string name = "Jenny";
cout << name;
Try it Yourself »
String Concatenation
You can combine (concatenate) strings using the +
operator.
first = "Hello"
last = "World"
message = first + " " + last
print(message)
const first = "Hello";
const last = "World";
const message = first + " " + last;
console.log(message);
String first = "Hello";
String last = "World";
String message = first + " " + last;
System.out.println(message);
string first = "Hello";
string last = "World";
string message = first + " " + last;
cout << message;
String Length
You can find out how many characters are in a string by using a built-in function or property.
txt = "Hello"
print(len(txt))
const txt = "Hello";
console.log(txt.length);
String txt = "Hello";
System.out.println(txt.length());
string txt = "Hello";
cout << txt.length();
Accessing Characters
Strings are like arrays of characters, so you can access a single character using an index (starting from 0).
txt = "Hello"
print(txt[1]) # prints 'e'
const txt = "Hello";
console.log(txt[1]); // prints 'e'
String txt = "Hello";
System.out.println(txt.charAt(1)); // prints 'e'
string txt = "Hello";
cout << txt[1]; // prints 'e'
Common String Operations
Some of the most common things we do with strings are:
- Convert to uppercase or lowercase
- Check if a string contains another string
- Replace text inside a string
txt = "Hello World"
print(txt.upper()) # HELLO WORLD
print(txt.lower()) # hello world
print("World" in txt) # True
print(txt.replace("World", "Python"))
const txt = "Hello World";
console.log(txt.toUpperCase()); // HELLO WORLD
console.log(txt.toLowerCase()); // hello world
console.log(txt.includes("World")); // true
console.log(txt.replace("World", "JavaScript"));
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // HELLO WORLD
System.out.println(txt.toLowerCase()); // hello world
System.out.println(txt.contains("World")); // true
System.out.println(txt.replace("World", "Java")); // Hello Java
#include <iostream>
#include <string>
#include <algorithm> // for transform
#include <cctype> // for toupper, tolower
using namespace std;
int main() {
string txt = "Hello World";
// Convert to uppercase
string upper_txt = txt;
transform(upper_txt.begin(), upper_txt.end(), upper_txt.begin(), ::toupper);
cout << upper_txt << endl; // HELLO WORLD
// Convert to lowercase
string lower_txt = txt;
transform(lower_txt.begin(), lower_txt.end(), lower_txt.begin(), ::tolower);
cout << lower_txt << endl; // hello world
// Check if string contains "World"
if (txt.find("World") != string::npos) {
cout << "true" << endl; // true
} else {
cout << "false" << endl;
}
// Replace "World" with "C++"
string replaced_txt = txt;
size_t pos = replaced_txt.find("World");
if (pos != string::npos) {
replaced_txt.replace(pos, 5, "C++");
}
cout << replaced_txt << endl; // Hello C++
return 0;
}
Note: Each programming language has its own set of string functions. The most common operations like finding length, accessing characters, and replacing text exist in almost all languages.
Real-Life Example
Strings are especially useful when working with user input. For example, let's say we are creating a program to greet the user by name:
name = "Jenny"
greeting = "Hello, " + name + "!"
print(greeting)
const name = "Jenny";
const greeting = "Hello, " + name + "!";
console.log(greeting);
String name = "Jenny";
String greeting = "Hello, " + name + "!";
System.out.println(greeting);
string name = "Jenny";
string greeting = "Hello, " + name + "!";
cout << greeting;