String Operations
In this page:
Concatenation
Concatenation joins two or more strings end to end into a single new string, which in some languages (like Java) creates a new string object each time rather than modifying the originals in place.
Example: Concatenation
#include <iostream>
using namespace std;
int main() {
string a = "Hello, ", b = "World!";
string result = a + b;
cout << result << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String a = "Hello, ", b = "World!";
String result = a + b;
System.out.println(result);
}
}
a, b = "Hello, ", "World!"
result = a + b
print(result)
#include <stdio.h>
#include <string.h>
int main() {
char a[20] = "Hello, ", b[] = "World!";
strcat(a, b);
printf("%s\n", a);
return 0;
}
Login to try C/C++/Java code in the editor
Comparison
Comparing strings checks whether two strings are exactly equal, or determines their relative order (which one would come first alphabetically), which matters for sorting words or validating user input against an expected value.
Example: Comparison
#include <iostream>
using namespace std;
int main() {
string a = "apple", b = "banana";
cout << (a == b ? "Equal" : "Not equal") << endl;
cout << (a < b ? "a comes first" : "b comes first") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String a = "apple", b = "banana";
System.out.println(a.equals(b) ? "Equal" : "Not equal");
System.out.println(a.compareTo(b) < 0 ? "a comes first" : "b comes first");
}
}
a, b = "apple", "banana"
print("Equal" if a == b else "Not equal")
print("a comes first" if a < b else "b comes first")
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "apple", b[] = "banana";
printf("%s\n", strcmp(a, b) == 0 ? "Equal" : "Not equal");
printf("%s\n", strcmp(a, b) < 0 ? "a comes first" : "b comes first");
return 0;
}
Login to try C/C++/Java code in the editor
Substring
A substring is a contiguous slice taken from within a larger string, defined by a starting position and either a length or an ending position, and it's one of the most frequently used string operations in real code.
Example: Substring
#include <iostream>
using namespace std;
int main() {
string s = "Hello, World!";
string sub = s.substr(7, 5);
cout << "Substring: " << sub << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "Hello, World!";
String sub = s.substring(7, 12);
System.out.println("Substring: " + sub);
}
}
s = "Hello, World!"
sub = s[7:12]
print("Substring:", sub)
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello, World!";
char sub[6];
strncpy(sub, s + 7, 5);
sub[5] = '\0';
printf("Substring: %s\n", sub);
return 0;
}
Login to try C/C++/Java code in the editor
Case Conversion
Case conversion changes every letter in a string to uppercase or lowercase, which is especially useful for case-insensitive comparisons, like checking a username regardless of how the user typed it.
Example: Case Conversion
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s = "Username123";
string lower = s;
transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
cout << "Lowercase: " << lower << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "Username123";
System.out.println("Lowercase: " + s.toLowerCase());
}
}
s = "Username123"
print("Lowercase:", s.lower())
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char s[] = "Username123";
for (int i = 0; i < strlen(s); i++) s[i] = tolower(s[i]);
printf("Lowercase: %s\n", s);
return 0;
}
Login to try C/C++/Java code in the editor
Search in String
Searching within a string checks whether a specific character or substring exists inside it, and often also returns the position where the first match was found, which is the building block for more advanced pattern-matching algorithms.
Example: Search in String
#include <iostream>
using namespace std;
int main() {
string s = "Hello, World!";
size_t pos = s.find("World");
cout << (pos != string::npos ? "Found at index " + to_string(pos) : "Not found") << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
String s = "Hello, World!";
int pos = s.indexOf("World");
System.out.println(pos != -1 ? "Found at index " + pos : "Not found");
}
}
s = "Hello, World!"
pos = s.find("World")
print(f"Found at index {pos}" if pos != -1 else "Not found")
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Hello, World!";
char *pos = strstr(s, "World");
if (pos) printf("Found at index %ld\n", pos - s);
else printf("Not found\n");
return 0;
}
Login to try C/C++/Java code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: