इस ट्यूटोरियल में, हम जावा स्ट्रिंग्स के बारे में जानेंगे, उन्हें कैसे बनाया जा सकता है, और उदाहरणों की मदद से स्ट्रिंग के विभिन्न तरीके।
जावा में, एक स्ट्रिंग वर्णों का एक क्रम है। उदाहरण के लिए, "हैलो" एक स्ट्रिंग है जिसमें 'h', 'e', 'l', 'l', और 'o' अक्षर होते हैं।
जावा में स्ट्रिंग का प्रतिनिधित्व करने के लिए हम दोहरे उद्धरण चिह्नों का उपयोग करते हैं। उदाहरण के लिए,
// create a string String type = "Java programming";
यहां, हमने एक स्ट्रिंग वैरिएबल बनाया है जिसका नाम टाइप है। चर स्ट्रिंग के साथ आरंभिक है Java Programming
।
नोट : जावा में स्ट्रिंग्स आदिम प्रकार (जैसे नहीं हैं int
, char
, आदि)। इसके बजाय, सभी तार नाम की एक पूर्वनिर्धारित कक्षा की वस्तु हैं String
।
और, सभी स्ट्रिंग चर String
वर्ग के उदाहरण हैं ।
उदाहरण: जावा में एक स्ट्रिंग बनाएं
class Main ( public static void main(String() args) ( // create strings String first = "Java"; String second = "Python"; String third = "JavaScript"; // print strings System.out.println(first); // print Java System.out.println(second); // print Python System.out.println(third); // print JavaScript ) )
उपरोक्त उदाहरण में, हमने पहले, दूसरे और तीसरे नाम के तीन तार बनाए हैं। यहां, हम सीधे आदिम प्रकारों की तरह तार पैदा कर रहे हैं।
हालाँकि, जावा स्ट्रिंग्स ( new
कीवर्ड का उपयोग करके ) बनाने का एक और तरीका है । हम इस बारे में बाद में इस ट्यूटोरियल में जानेंगे।
जावा स्ट्रिंग ऑपरेशन
जावा स्ट्रिंग स्ट्रिंग्स पर विभिन्न ऑपरेशन करने के लिए विभिन्न तरीके प्रदान करता है। हम आमतौर पर उपयोग किए जाने वाले कुछ स्ट्रिंग ऑपरेशनों पर ध्यान देंगे।
1. एक स्ट्रिंग की लंबाई प्राप्त करें
एक स्ट्रिंग की लंबाई खोजने के लिए, हम स्ट्रिंग की length()
विधि का उपयोग करते हैं । उदाहरण के लिए,
class Main ( public static void main(String() args) ( // create a string String greet = "Hello! World"; System.out.println("String: " + greet); // get the length of greet int length = greet.length(); System.out.println("Length: " + length); ) )
आउटपुट
स्ट्रिंग: हैलो! दुनिया की लंबाई: 12
उपरोक्त उदाहरण में, length()
विधि स्ट्रिंग में वर्णों की कुल संख्या की गणना करती है और इसे वापस करती है। अधिक जानने के लिए, जावा स्ट्रिंग लंबाई () पर जाएँ।
2. दो स्ट्रिंग्स से जुड़ें
हम concat()
विधि का उपयोग करके जावा में दो तार जोड़ सकते हैं । उदाहरण के लिए,
class Main ( public static void main(String() args) ( // create first string String first = "Java "; System.out.println("First String: " + first); // create second String second = "Programming"; System.out.println("Second String: " + second); // join two strings String joinedString = first.concat(second); System.out.println("Joined String: " + joinedString); ) )
आउटपुट
पहला स्ट्रिंग: जावा दूसरा स्ट्रिंग: प्रोग्रामिंग स्ट्रिंग में शामिल हो गया: जावा प्रोग्रामिंग
उपरोक्त उदाहरण में, हमने पहले और दूसरे नाम के दो तार बनाए हैं। कथन पर ध्यान दें,
String joinedString = first.concat(second);
यहां, हम concat()
विधि पहले और दूसरे से जुड़ते हैं और इसे joinString वेरिएबल में असाइन करते हैं।
हम +
जावा में ऑपरेटर का उपयोग करके दो तार भी जोड़ सकते हैं । अधिक जानने के लिए, जावा स्ट्रिंग कॉनसैट () पर जाएँ।
3. दो स्ट्रिंग्स की तुलना करें
जावा में, हम equals()
विधि का उपयोग करके दो तारों के बीच तुलना कर सकते हैं । उदाहरण के लिए,
class Main ( public static void main(String() args) ( // create 3 strings String first = "java programming"; String second = "java programming"; String third = "python programming"; // compare first and second strings boolean result1 = first.equals(second); System.out.println("Strings first and second are equal: " + result1); // compare first and third strings boolean result2 = first.equals(third); System.out.println("Strings first and third are equal: " + result2); ) )
आउटपुट
स्ट्रिंग्स पहले और दूसरे बराबर हैं: सच्चे स्ट्रिंग्स पहले और तीसरे बराबर हैं: झूठे
उपरोक्त उदाहरण में, हमने पहले, दूसरे और तीसरे नाम के 3 तार बनाए हैं। यहां, हम equal()
यह जांचने के लिए विधि का उपयोग कर रहे हैं कि क्या एक स्ट्रिंग दूसरे के बराबर है।
equals()
विधि चेकों तार की सामग्री, जबकि उनकी तुलना। अधिक जानने के लिए, जावा स्ट्रिंग बराबरी () पर जाएँ।
नोट : हम ==
जावा में ऑपरेटर का उपयोग करके दो तारों की तुलना भी कर सकते हैं । हालांकि, यह दृष्टिकोण equals()
विधि की तुलना में अलग है । अधिक जानने के लिए, जावा स्ट्रिंग == बनाम बराबर () पर जाएँ।
जावा स्ट्रिंग के तरीके
ऊपर वर्णित लोगों के अलावा, जावा में विभिन्न स्ट्रिंग विधियां मौजूद हैं। यहाँ उन विधियों में से कुछ हैं:
तरीके | विवरण |
---|---|
प्रतिस्थापन () | स्ट्रिंग का विकल्प देता है |
बदलने के() | निर्दिष्ट पुराने चरित्र को नए वर्ण के साथ बदल देता है |
charAt () | निर्दिष्ट स्थान में मौजूद वर्ण देता है |
getBytes () | स्ट्रिंग को बाइट्स की एक सरणी में कनवर्ट करता है |
के सूचकांक() | स्ट्रिंग में निर्दिष्ट वर्ण की स्थिति देता है |
से तुलना करें() | शब्दकोश क्रम में दो तारों की तुलना करता है |
ट्रिम () | किसी भी प्रमुख और अनुगामी व्हाट्सएप को हटा देता है |
प्रारूप () | एक स्वरूपित स्ट्रिंग देता है |
विभाजित करें() | स्ट्रिंग को स्ट्रिंग के एक सरणी में तोड़ता है |
toLowerCase () | स्ट्रिंग को लोअरकेस में कनवर्ट करता है |
टॉपरकैस () | स्ट्रिंग को अपरकेस में परिवर्तित करता है |
का मूल्य() | निर्दिष्ट तर्क का स्ट्रिंग प्रतिनिधित्व लौटाता है |
करहर्रे () | स्ट्रिंग को एक char सरणी में परिवर्तित करता है |
जावा स्ट्रिंग्स में चरित्र से बच
एस्केप कैरेक्टर का इस्तेमाल स्ट्रिंग के अंदर मौजूद कुछ कैरेक्टर्स से बचने के लिए किया जाता है।
मान लीजिए हमें एक स्ट्रिंग के अंदर दोहरे उद्धरण चिह्नों को शामिल करने की आवश्यकता है।
// include double quote String example = "This is the "String" class";
Since strings are represented by double quotes, the compiler will treat "This is the " as the string. Hence, the above code will cause an error.
To solve this issue, we use the escape character in Java. For example,
// use the escape character String example = "This is the "String " class.";
Now escape characters tell the compiler to escape double quotes and read the whole text.
Java Strings are Immutable
In Java, strings are immutable. This means, once we create a string, we cannot change that string.
To understand it more deeply, consider an example:
// create a string String example = "Hello! ";
Here, we have created a string variable named example. The variable holds the string "Hello! ".
Now suppose we want to change the string.
// add another string "World" // to the previous tring example example = example.concat(" World");
Here, we are using the concat()
method to add another string World to the previous string.
It looks like we are able to change the value of the previous string. However, this is not true
.
Let's see what has happened here,
- JVM takes the first string "Hello! "
- creates a new string by adding "World" to the first string
- assign the new string "Hello! World" to the example variable
- the first string "Hello! " remains unchanged
Creating strings using the new keyword
So far we have created strings like primitive types in Java.
Since strings in Java are objects, we can create strings using the new
keyword as well. For example,
// create a string using the new keyword String name = new String("Java String");
In the above example, we have created a string name using the new
keyword.
Here, when we create a string object, the String()
constructor is invoked. To learn more about constructor, visit Java Constructor.
Note: The String
class provides various other constructors to create strings. To learn more, visit Java String (official Java documentation).
Example: Create Java Strings using the new keyword
class Main ( public static void main(String() args) ( // create a string using new String name = new String("Java String"); System.out.println(name); // print Java String ) )
Create String using literals vs new keyword
Now that we know how strings are created using string literals and the new
keyword, let's see what is the major difference between them.
In Java, the JVM maintains a string pool to store all of its strings inside the memory. The string pool helps in reusing the strings.
While creating strings using string literals, the value of the string is directly provided. Hence, the compiler first checks the string pool to see if the string already exists.
- यदि स्ट्रिंग पहले से मौजूद है , तो नया स्ट्रिंग नहीं बनाया गया है। इसके बजाय, नया संदर्भ मौजूदा स्ट्रिंग को इंगित करता है।
- यदि स्ट्रिंग मौजूद नहीं है , तो नया स्ट्रिंग बनाया गया है।
हालाँकि, नए कीवर्ड का उपयोग करते हुए स्ट्रिंग्स बनाते समय , स्ट्रिंग का मान सीधे प्रदान नहीं किया जाता है। इसलिए हर समय नया तार बनाया जाता है।