← Back to DSA Course | Chapter 3: Strings | Lesson 2 of 6

String के Operations

String operations वे चीज़ें हैं जो आप letters के necklaces के साथ करते हैं: दो को एक साथ जोड़ना, pieces काटना, या किसी छोटे piece को अंदर ढूंढना।
Syntax
markup
combined = string1 + string2
equal = string1 == string2
part = text[start:end]

Concatenation

Concatenation दो या ज़्यादा strings को end to end एक single नई string में जोड़ता है, जो कुछ languages में (जैसे Java) हर बार originals को जगह पर modify करने के बजाय एक नया string object बनाता है।

उदाहरण: 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;
}

Comparison

Strings compare करना जांचता है कि दो strings बिल्कुल बराबर हैं या नहीं, या उनका relative order determine करता है (कौन सी alphabetically पहले आएगी), जो words sort करने या user input को एक expected value के खिलाफ validate करने के लिए मायने रखता है।

उदाहरण: 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;
}

Substring

एक substring एक बड़ी string के अंदर से लिया गया एक contiguous slice है, एक starting position और एक length या ending position से defined, और यह असली code में सबसे ज़्यादा उपयोग होने वाले string operations में से एक है।

उदाहरण: 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;
}

Case Conversion

Case conversion किसी string में हर letter को uppercase या lowercase में बदलता है, जो case-insensitive comparisons के लिए विशेष रूप से उपयोगी है, जैसे user ने कैसे भी type किया हो एक username जांचना।

उदाहरण: 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;
}

Search in String

किसी string के अंदर search करना जांचता है कि क्या एक specific character या substring इसके अंदर मौजूद है, और अक्सर यह position भी return करता है जहां पहला match मिला, जो ज़्यादा advanced pattern-matching algorithms के लिए building block है।

उदाहरण: 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. C-style strings को == से compare करना, जो contents के बजाय pointers compare करता है (C++ std::string इसे properly handle करता है, char* नहीं)।
  2. बहुत बड़ी strings के लिए एक loop में s = s + x से concatenate करना, जो हर बार string copy करता है।
  3. गलत दूसरे argument के साथ एक substring लेना, क्योंकि s.substr(pos, len) एक length लेता है, एक end index नहीं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.