Link Search Menu Expand Document

String Algorithms

Strings are the de facto way of storing text in a program. They are arrays (lists) of characters, usually enclosed in single or double quotes (or either) depending on the language. Examples: "Hello World!", 'Hello World!'.

Basic String Operations

Some basic string operations will be defined below, followed by code examples for the language that you’ve selected at the top of this page.

1. Get length of string

Code

my_str = "hello"
# 1. Get length of string
print(len(my_str))
String myStr = "hello";
// 1. Get length of string
System.out.println(myStr.length())
const myStr = "hello";
// 1. Get length of string
console.log(myStr.length)

Output

5

2. Get character by index

Because strings are essentially arrays of characters, you can retrieve a single character by its index (position) in the string. The index starts at 0 on the leftmost character.

string: hello
index:  01234
length of string: 5

Code

my_str = "hello"
# 2. Get character by index
print(my_str[0])
print(my_str[1])
print(my_str[2])
print(my_str[-2])
print(my_str[-1]) # alternative: my_str[len(my_str) - 1]
String myStr = "hello";
// 2. Get character by index
System.out.println(myStr.charAt(0));
System.out.println(myStr.charAt(1));
System.out.println(myStr.charAt(2));
System.out.println(myStr.charAt(myStr.length() - 2));
System.out.println(myStr.charAt(myStr.length() - 1));
const myStr = "hello"
// 2. Get character by index
console.log(myStr.charAt(0)) // alternative: myStr[0]
console.log(myStr.charAt(1)) // alternative: myStr[1]
console.log(myStr.charAt(2)) // etc.
console.log(myStr.charAt(myStr.length - 2))
console.log(myStr.charAt(myStr.length - 1))

Output

h
e
l
l
o

3. Get a substring

my_str = "hello"
# 3. Get a substring (aka a string slice)
print(my_str[1:])
print(my_str[0:4])
print(my_str[1:2])
print(my_str[:2])
String myStr = "hello";
// 3. Get a substring
System.out.println(myStr.substring(1));
System.out.println(myStr.substring(0, 4));
System.out.println(myStr.substring(1, 2));
System.out.println(myStr.substring(0, 2));
const myStr = "hello"
// 3. Get a substring
console.log(myStr.substring(1))
console.log(myStr.substring(0, 4))
console.log(myStr.substring(1, 2))
console.log(myStr.substring(0, 2))

Output

ello
hell
e
he

4. Find index of substring

Code

my_str = "hello"
# 4. Find index of substring
print(my_str.index("l"))
print(my_str.index("l", 3)) # the second parameter is what index we start searching from
print(my_str.index("el"))
print(my_str.index("help")) # not found, throws a runtime error
String myStr = "hello";
// 4. Find index of substring
System.out.println(myStr.indexOf("l"));
System.out.println(myStr.indexOf("l", 3)); // the second parameter is what index we start searching from
System.out.println(myStr.indexOf("el"));
System.out.println(myStr.indexOf("help"));
const myStr = "hello"
// 4. Find index of substring
console.log(myStr.indexOf("l"))
console.log(myStr.indexOf("l", 3)) // the second parameter is what index we start searching from
console.log(myStr.indexOf("el"))
console.log(myStr.indexOf("help"))
2
3
1
-1 # Java and JS return -1 for not found, Python throws a "ValueError" exception.