Java Strings

Strings का उपयोग किसी भी text को स्टोर करने के लिए किया जाता है, और String का value हमेसा double quotes " " से घिरा हुआ होता है। Java में, strings को object के रूप में माना जाता है।

Example: Java Strings
public class MyClass {

  public static void main(String[] args) {
    String text = "Hello World";

    System.out.println(text);
  }
}
// Result: Hello World

Java String Methods

जैसा कि हम जानते हैं कि Java Strings वास्तव में एक object है, जिसमें बहुत सारी methods होती हैं, जो String पर कुछ कार्य को perform करती हैं, जैसे कि length(), toUpperCase(), toLowerCase(), indexOf() इत्यादि। Java methods के बारे में बिस्तार में हम Java string method चैप्टर में देखिंगे।

  1. length() : String की लम्बाई पता करने के लिए
  2. toUpperCase() : String को UPPERCASE में बदलने के लिए
  3. toLowerCase() : String को lowercase में बदलने के लिए
  4. indexOf() : String में किसी भी charactor की position पता करने के लिए
Example: Java Strings Methods
public class MyClass {

  public static void main(String[] args) {
    String text = "HELLO World";

    System.out.println(text.length());
    System.out.println(text.toUpperCase());
    System.out.println(text.toLowerCase());
    System.out.println(text.indexOf('W'));
  }
}
// Result for length() : 11
// Result for toUpperCase() : HELLO WORLD
// Result for toLowerCase() : hello world
// Result for indexOf() : 6
नोट: Java खाली स्थान अर्थात whitespace को भी count करता हैं, Java में गिनती 0 से शुरु होती हैं, अर्थात 0123456..... न की 123456….

String Concatenation

दो या दो से अधिक strings को जोड़ने को ही Concatenation कहा जाता हैं, Concatenation निचे दिए गए दो methods से किया जाता हैं।

  1. + Operator की उपयोग करके।
  2. concet() method का उपयोग करके।
Example: Java Strings Concatenation
public class MyClass {

  public static void main(String[] args) {
    String text1 = "Ram";
    String text2 = "Singh";

    System.out.println(text1 + text2);
    System.out.println(text1 + " " + text2);
    System.out.println(text1.concat(text2));
    System.out.println(text1 + " ".concat(text2));
  }
}
// Result: RamSingh
// Result: Ram Singh
// Result: RamSingh
// Result: Ram Singh

उदाहरण का सारांश

1st Result: दोनों strings Ram और Singh को जोड़ा गया है + operator की मदद से।

2nd Result: Ram और Singh के बीच में खाली स्थान है क्योंकि एक खाली स्थान double quote " " के अंदर है और दोनों strings को + ऑपरेटर की मदद से जोड़ा गया है।

3rd Result: दोनों strings Ram और Singh को जोड़ा गया है concet() method की मदद से। और दोनों strings के बीच कोई भी खाली स्थान नहीं है क्योंकि हमने कोई भी खाली स्थान का इस्तेमाल नहीं किया है इस method में।

4th Result: इसमें हमने + operator और concet() method दोनों का इस्तेमाल किया है।

नोट: जावा में + operator का इस्तेमाल additionऔर concatenation दोनों के लिए किया जाता हैं।
  • int (number), के लिए + operator का इस्तेमाल addition Arithmetic operators के लिए किया जाता हैं।
  • Strings, के लिए + operator का इस्तेमाल concatenation के लिए किया जाता हैं।

Special Characters

जैसा की हम जानते है की एक strings हमेशा double quotes के अन्दर लिखा जाता है।
मान लीजये किसी सेंटेंस में आपको single quote या double quote का इस्तेमाल करना है तो आप क्या करेंगे? अगर आप ने डायरेक्ट single quote या double quote का इस्तेमाल किया तो Java इस स्ट्रिंग को गलत समझेगा, और एक error मेसेज generate करेगा, जैसा की आप नीचे एक example में देखेंगे।

Example
public class MyClass {

  public static void main(String[] args) {
    String text = "I want "Milk" food";
  }
}

इस तरह की गलती से बचने के लिए हम backslash \ का इस्तेमाल करते हैं, जो एक एक स्पेशल Character को स्ट्रिंग्स Character में बदल देता है। जावा में इसे backslash escape character कहते है।

Escape Character Result
" Double quote
\ Backslash
n New Line
r Carriage Return
t Tab
b Backspace
Example
public class MyClass {

  public static void main(String[] args) {
    String text1 = "I want \"Milk\" food.";
    String text2 = "This is \\backslash.";
    String text3 = "Return only \r after character.";
    String text4 = "Tab space \t in between.";
    String text5 = "Backspace wron\bng statements.";
    String text6 = "For new \n line.";

    System.out.println(text1);
    // Result is I want "Milk" food.

    System.out.println(text2);
    // Result is This is \backslash

    System.out.println(text3);
    // Result is after character.

    System.out.println(text4);
    // Result is Tab space in between.

    System.out.println(text5);
    // Result is Backspace wrong character.

    System.out.println(text6);
    /* Result is For new
  line.*/
  }
}
Article By: Brajlal Prasad
Created on: 17 Feb 2023  5643  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]