String Functions
Common Pseudocode provides a comprehensive set of functions for working with strings.
String Properties
Section titled “String Properties”length - Get String Length
Section titled “length - Get String Length”Returns the number of characters in a string.
Syntax:
result = length of textresult = text.lengthExample:
text = "Hello World"textLength = length of text // 11count = text.length // 11Case Conversion
Section titled “Case Conversion”to uppercase - Convert to Uppercase
Section titled “to uppercase - Convert to Uppercase”Converts all characters in a string to uppercase.
Syntax:
result = text to uppercaseExample:
text = "Hello World"uppercase = text to uppercase // "HELLO WORLD"to lowercase - Convert to Lowercase
Section titled “to lowercase - Convert to Lowercase”Converts all characters in a string to lowercase.
Syntax:
result = text to lowercaseExample:
text = "Hello World"lowercase = text to lowercase // "hello world"Substring Operations
Section titled “Substring Operations”substring - Extract Substring
Section titled “substring - Extract Substring”Extracts a portion of a string from a start index to an end index.
Syntax:
result = substring of text from start to endExample:
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.
Character Access
Section titled “Character Access”Access individual characters using array-like indexing.
Syntax:
char = text[index]Example:
text = "Hello World"firstChar = text[0] // "H"lastChar = text[10] // "d"String Searching
Section titled “String Searching”find - Find Substring Position
Section titled “find - Find Substring Position”Finds the position of a substring within a string.
Syntax:
position = find substring in textExample:
text = "Hello World"position = find "World" in text // returns 6position = 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 substringExample:
text = "Hello World"hasWorld = text contains "World" // returns truehasJava = text contains "Java" // returns false
if text contains "Hello" output "Greeting found!"String Manipulation
Section titled “String Manipulation”trim - Remove Whitespace
Section titled “trim - Remove Whitespace”Removes leading and trailing whitespace from a string.
Syntax:
result = trim(text)Example:
text = " Hello World "trimmed = trim(text) // "Hello World"split - Split String into Array
Section titled “split - Split String into Array”Splits a string into an array of substrings based on a delimiter.
Syntax:
result = split text by delimiterExample:
text = "Hello World"words = split text by " " // ["Hello", "World"]
csv = "apple,banana,orange"fruits = split csv by "," // ["apple", "banana", "orange"]join - Join Array into String
Section titled “join - Join Array into String”Joins an array of strings into a single string with a delimiter.
Syntax:
result = join array with delimiterExample:
words = ["Hello", "World"]text = join words with " " // "Hello World"
fruits = ["apple", "banana", "orange"]csv = join fruits with "," // "apple,banana,orange"String Concatenation
Section titled “String Concatenation”Basic Concatenation
Section titled “Basic Concatenation”Join strings together by placing them next to each other.
Syntax:
result = "string1" "string2" "string3"Example:
greeting = "Hello" " " "World" // "Hello World"String Interpolation
Section titled “String Interpolation”Embed variables in strings using curly braces.
Syntax:
result = "text {variable} more text"Example:
num = 10message = "Hello the number is {num}" // "Hello the number is 10"
name = "Alice"age = 30greeting = "My name is {name} and I am {age} years old"// "My name is Alice and I am 30 years old"Type Conversion
Section titled “Type Conversion”convert to string
Section titled “convert to string”Convert other data types to strings.
Syntax:
stringValue = convert value to stringstringValue = string(value)Example:
age = 25stringAge = convert age to string // "25"stringAge = string(age) // "25"Common String Operations Example
Section titled “Common String Operations Example”// Get user inputoutput "Enter your full name: "input fullName
// Clean and process the namefullName = trim(fullName)fullName = fullName to uppercase
// Split into first and last namenameParts = 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"}