Java Strings – Working with Text in Java जावा स्ट्रिंग्स

In Java, a string is a sequence of characters. For example, "Java Book" is a string. Strings in Java are defined as objects so that various operations can be performed on them like concatenation, comparison, and extraction. The String class allows us to create string variables or objects and perform operations on them.

जावा में, एक स्ट्रिंग अक्षरों (characters) का एक क्रम होती है। जैसे कि "जावा की किताब" एक स्ट्रिंग है। जावा में स्ट्रिंग को ऑब्जेक्ट के रूप में परिभाषित किया गया है ताकि हम उस पर विभिन्न ऑपरेशन्स जैसे जोड़ना, तुलना करना आदि कर सकें। String क्लास की मदद से हम सामान्य स्ट्रिंग वेरिएबल या ऑब्जेक्ट बना सकते हैं और उन पर ऑपरेशन कर सकते हैं।

String myString = "this is a string"; // variable
String myString = new String("This is a string"); // object

✨ Common Methods of Java String Class जावा स्ट्रिंग क्लास की सामान्य मेथड्स:

1. equals() – String Comparison

Used to compare two strings. If they are equal, it returns true.

दो स्ट्रिंग्स की तुलना के लिए equals() मेथड का प्रयोग किया जाता है। यदि दोनों समान हों तो यह true लौटाता है।

String s1 = "hello world";
String s2 = "hello world";
if(s1.equals(s2)) {
System.out.println("Both Strings are Equal.");
}

2. toUpperCase() and toLowerCase() – Change Case

These methods convert the string to uppercase or lowercase.

ये मेथड्स किसी स्ट्रिंग को कैपिटल या स्मॉल अक्षरों में बदलने के लिए उपयोग होते हैं।

String str1 = "gooDMoRniNg";
System.out.println(str1.toUpperCase()); // GOODMORNING
System.out.println(str1.toLowerCase()); // goodmorning

3. startsWith() and endsWith() – Check Prefix/Suffix

Used to test whether the string starts or ends with a particular word.

इनका उपयोग यह जांचने के लिए किया जाता है कि स्ट्रिंग किसी विशेष शब्द से शुरू या समाप्त होती है या नहीं।

String s1 = "hello world";
if(s1.startsWith("hello")) System.out.println("Starts with hello");
if(s1.endsWith("world")) System.out.println("Ends with world");

4. length() – Get String Length

Returns the number of characters in the string.

स्ट्रिंग की लम्बाई यानी करैक्टर की संख्या को जानने के लिए length() मेथड प्रयोग की जाती है।

String myString = "Hello World";
System.out.println(myString.length()); // 11

5. concat() – String Concatenation

Used to join two strings either with + or concat() method.

दो स्ट्रिंग्स को जोड़ने के लिए + या concat() मेथड का प्रयोग किया जाता है।

String s1 = "Hello ";
String s2 = "World";
String result = s1.concat(s2); // Hello World
System.out.println(result);

6. charAt() – Access Specific Character

Returns the character at the specified index.

यह मेथड किसी दिए गए इंडेक्स पर मौजूद करैक्टर को लौटाता है।

String s = "hello";
System.out.println(s.charAt(1)); // e

7. substring() – Extract Part of String

Extracts a part of the string from a given start index to end index.

इसका उपयोग स्ट्रिंग का कोई भाग प्राप्त करने के लिए किया जाता है।

String s = "Sitaram";
System.out.println(s.substring(2, 5)); // tar

8. indexOf() – Find Position of Substring

Returns the starting index of a substring within a string.

यह सबस्ट्रिंग के पहले अक्षर की पोजीशन (index) लौटाता है।

String s = "hello my name is khan";
System.out.println(s.indexOf("my")); // 6

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध