Strings: An Overview

Overview

Fundamentally, a string is a sequence of characters. Different languages implement them in different ways, but the basic string types in object-oriented languages tend to be immutable. This means you cannot directly modify them at all and must create a new object to hold any variation on the string.

When you're modifying a string a lot, it can get pretty costly in time and space to repeatedly recreate minor variants of the same string over and over. So what do you do when you have a real use-case for a mutable string? Some languages have additional classes that allow you to manipulate strings. For example, Java's String class is immutable, but you can use StringBuffer or StringBuilder for scenarios where you truly need a mutable sequence of characters.

Concatenation

When we concatenate two strings, we join them together so that the end of one string is followed by the beginning of the other or, more simply put, we append one string to another. Most languages use the + operator for concatenating strings.

Let's say we have two strings, and . If we were to concatenate them and store the result in some third string, , we would get .

EXAMPLE
The Java code below demonstrates concatenating two strings.
public class Example {
	public static void main(String[] args) {
    	String a = "Hello";
        String b = "World";
        String c = a + b;
        System.out.println(c); 
    }
}
Run
Output

While using and to create is very simple in this example, concatenation of immutable strings can get very costly in instances where you're making many minor modifications to the same string. If you need to perform repeated concatenations, you may want to consider using a mutable string class (or implementing your own).

 
Go to Top
  1. Challenge Walkthrough
    Let's walk through this sample challenge and explore the features of the code editor.1 of 6
  2. Review the problem statement
    Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
  3. Choose a language
    Select the language you wish to use to solve this challenge.3 of 6
  4. Enter your code
    Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
  5. Test your code
    You can compile your code and test it for errors and accuracy before submitting.5 of 6
  6. Submit to see results
    When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
  1. Check your score