Skip to content

String Functions

Common Pseudocode provides a comprehensive set of functions for working with strings.

Returns the number of characters in a string.

Syntax:

result = length of text
result = text.length

Example:

text = "Hello World"
textLength = length of text // 11
count = text.length // 11

Converts all characters in a string to uppercase.

Syntax:

result = text to uppercase

Example:

text = "Hello World"
uppercase = text to uppercase // "HELLO WORLD"

Converts all characters in a string to lowercase.

Syntax:

result = text to lowercase

Example:

text = "Hello World"
lowercase = text to lowercase // "hello world"

Extracts a portion of a string from a start index to an end index.

Syntax:

result = substring of text from start to end

Example:

text = "Hello World"
substring = substring of text from 0 to 5 // "Hello"
substring = substring of text from 6 to 11 // "World"

Note: Indices are 0-based, similar to arrays.


Access individual characters using array-like indexing.

Syntax:

char = text[index]

Example:

text = "Hello World"
firstChar = text[0] // "H"
lastChar = text[10] // "d"

Finds the position of a substring within a string.

Syntax:

position = find substring in text

Example:

text = "Hello World"
position = find "World" in text // returns 6
position = find "o" in text // returns 4 (first occurrence)

Returns: The index of the first occurrence of the substring, or -1 if not found.


contains - Check if String Contains Substring

Section titled “contains - Check if String Contains Substring”

Checks whether a string contains a specific substring.

Syntax:

result = text contains substring

Example:

text = "Hello World"
hasWorld = text contains "World" // returns true
hasJava = text contains "Java" // returns false
if text contains "Hello"
output "Greeting found!"

Removes leading and trailing whitespace from a string.

Syntax:

result = trim(text)

Example:

text = " Hello World "
trimmed = trim(text) // "Hello World"

Splits a string into an array of substrings based on a delimiter.

Syntax:

result = split text by delimiter

Example:

text = "Hello World"
words = split text by " " // ["Hello", "World"]
csv = "apple,banana,orange"
fruits = split csv by "," // ["apple", "banana", "orange"]

Joins an array of strings into a single string with a delimiter.

Syntax:

result = join array with delimiter

Example:

words = ["Hello", "World"]
text = join words with " " // "Hello World"
fruits = ["apple", "banana", "orange"]
csv = join fruits with "," // "apple,banana,orange"

Join strings together by placing them next to each other.

Syntax:

result = "string1" "string2" "string3"

Example:

greeting = "Hello" " " "World" // "Hello World"

Embed variables in strings using curly braces.

Syntax:

result = "text {variable} more text"

Example:

num = 10
message = "Hello the number is {num}" // "Hello the number is 10"
name = "Alice"
age = 30
greeting = "My name is {name} and I am {age} years old"
// "My name is Alice and I am 30 years old"

Convert other data types to strings.

Syntax:

stringValue = convert value to string
stringValue = string(value)

Example:

age = 25
stringAge = convert age to string // "25"
stringAge = string(age) // "25"

// Get user input
output "Enter your full name: "
input fullName
// Clean and process the name
fullName = trim(fullName)
fullName = fullName to uppercase
// Split into first and last name
nameParts = split fullName by " "
if length of nameParts >= 2 {
firstName = nameParts[0]
lastName = nameParts[length of nameParts - 1]
output "First: {firstName}, Last: {lastName}"
} else {
output "Please enter both first and last name"
}