DSA Strings
Learn how strings store text and how to access, traverse, combine, search, and compare them.
Overview
Strings are among the most frequently used data types in programming. Names, messages, passwords, email addresses, and search queries are all strings. In DSA, string problems commonly involve searching, comparing, modifying, or analyzing text.
Key concepts
- A string is a sequence of characters stored as one value
- Characters can include letters, numbers, symbols, and spaces
- Every character has an index that usually begins at zero
- String skills are essential for real-world applications and coding interviews
What is a String?
A string is a sequence of characters stored together as a single value. Knowing a character's index allows you to access it directly.
String: H e l l o
Index: 0 1 2 3 4Examples of strings include "Hello", "JavaScript", "DSA", "Welcome to DevBrainBox", and even numeric text such as "12345".
Why are Strings important?
- User names and passwords
- Email addresses
- Chat messages
- Search queries
- Website URLs
- File names
- Product descriptions
Text is central to modern software, so understanding how to process strings is an essential programming skill.
Creating a String
JavaScript strings can use single quotes, double quotes, or backticks.
const message = "Welcome to DevBrainBox";
console.log(message);
// Welcome to DevBrainBoxAccessing characters
Use square brackets and an index to access an individual character. Like arrays, strings normally use zero-based indexing.
const language = "JavaScript";
console.log(language[0]); // J
console.log(language[4]); // SFinding the length of a String
The length property returns the number of characters. It is useful when traversing text or validating input.
const word = "Developer";
console.log(word.length); // 9Traversing a String
Traversing means visiting every character. It is commonly used to count characters, check conditions, or transform text.
const word = "Code";
for (let i = 0; i < word.length; i++) {
console.log(word[i]);
}Common String operations
- Access characters
- Find the length
- Compare strings
- Join strings
- Extract part of a string
- Change letter case
- Search for text
- Replace characters or words
Concatenating Strings
Concatenation joins two or more strings and is frequently used to create messages or display user information.
const firstName = "John";
const lastName = "Doe";
const fullName = firstName + " " + lastName;
console.log(fullName); // John DoeSearching within a String
JavaScript's includes method checks whether a string contains specific text. String searching is useful in search engines, chat filters, and form validation.
const sentence = "Learning DSA is fun.";
console.log(sentence.includes("DSA")); // trueTime complexity of common operations
| Operation | Time complexity |
|---|---|
| Access character | O(1) |
| Find length | O(1) |
| Traverse string | O(n) |
| Search text | O(n) |
| Concatenate strings | O(n) |
| Compare strings | O(n) |
Here, n represents the number of characters. Understanding these costs is especially valuable when processing large amounts of text.
Real-life examples
A contacts application compares characters while searching for a friend's name. A login system compares the entered username and password with stored strings before allowing access. Both everyday tasks depend on string operations.
Where are Strings used?
- Search engines
- Email validation
- Online forms
- Social media posts
- Chat applications
- Password verification
- Text editors
- File management systems
Tips for beginners
- Remember that indexing begins at zero
- Practice accessing characters by index
- Use loops to understand text processing
- Learn length, includes(), slice(), toUpperCase(), and toLowerCase()
- Solve small string problems regularly
Key takeaways
- A string is a sequence of characters used to represent text
- Every character has an index
- Common operations include access, traversal, search, comparison, and concatenation
- Character access is O(1), while traversal and search are generally O(n)
- String manipulation supports both real applications and coding interviews