Skip to main content
Ajay-Dhangar
EditReport

String Operations

1. String Length

The length of a string refers to the total number of characters it contains, including spaces and special characters. Knowing the length of a string is useful in various scenarios, such as looping through each character or setting constraints on input.

Example:

  • Python:

    text = "Hello, World!"
    print(len(text)) # Output: 13
  • JavaScript:

    let text = "Hello, World!";
    console.log(text.length); // Output: 13
  • Java:

    String text = "Hello, World!";
    System.out.println(text.length()); // Output: 13

2. Concatenation of Strings

Concatenation is the process of joining two or more strings together to form a single combined string. This can be done using operators like + or dedicated methods, depending on the language.

Example:

first = "Hello"
second = "World"
result = first + " " + second # Output: "Hello World"
let first = "Hello";
let second = "World";
let result = first + " " + second; // Output: "Hello World"
String first = "Hello";
String second = "World";
String result = first.concat(" ", second); // Output: "Hello World"

3. Substring Operations

Substring operations allow you to extract a part of a string from a specific position (start) up to another position (end). This is often used to get specific information from a larger text.

Example:

text = "Hello, World!"
print(text[0:5]) # Output: "Hello"

4. String Comparison

String comparison is used to determine whether two strings are identical or, in some cases, to sort them alphabetically. Most languages provide comparison operators or methods to check for equality or order.

Example:

str1 = "Hello"
str2 = "World"
print(str1 == str2) # Output: False

String search is used to find whether a particular substring exists within a string, and if so, at what position. Different methods allow for both simple and more advanced searches.

Example:

text = "Hello, World!"
print(text.find("World")) # Output: 7

6. Case Conversion

Case conversion methods allow you to change a string to either uppercase or lowercase, which can be useful for standardizing text inputs or comparing strings in a case-insensitive manner.

Example:

text = "Hello, World!"
print(text.upper()) # Output: "HELLO, WORLD!"
print(text.lower()) # Output: "hello, world!"

7. String Replacement

Replacing characters or entire substrings allows you to create a new modified string based on the original. This operation is often used to format strings or correct user input.

Example:

text = "Hello, World!"
print(text.replace("World", "Python")) # Output: "Hello, Python!"

8. Splitting Strings into Arrays

Splitting a string converts it into an array of substrings based on a specified delimiter, which is useful for processing text data where elements are separated by spaces, commas, or other characters.

Example:

text = "Hello, World!"
print(text.split(", ")) # Output: ['Hello', 'World!']

9. String Padding

Padding is used to adjust the length of a string by adding extra characters to either the beginning or the end. It’s especially useful in formatting for aligned output or fixed-width fields.

Example:

text = "5"
print(text.zfill(3)) # Output: "005"
Finished reading? Mark this topic as complete.