Java String के Methods
In this page:
string.length();
string.substring(start, end);
string.equals(other);
string.toUpperCase();
Changing String Case
toUpperCase() और toLowerCase() हर letter case-converted वाला एक नया String return करते हैं, original string object unchanged छोड़ते हुए क्योंकि strings जगह पर modify नहीं हो सकतीं।
यह उन्हें equals() के साथ combined होने पर case-insensitive comparisons के लिए convenient बनाता है।
उदाहरण: Changing String Case
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "Hello"
String text = "Hello";
// Print a line to the console
System.out.println(text.toUpperCase());
// Print a line to the console
System.out.println(text.toLowerCase());
}
}
Login to try C/C++/Java/PHP code in the editor
Comparing Strings Correctly
String content को कभी == से compare न करें, क्योंकि यह जांचता है कि दो references बिल्कुल उसी object की ओर point करते हैं या नहीं बजाय उनके characters match करने के — अलग तरह से बनी दो strings (जैसे concatenation के ज़रिए) identical text रख सकती हैं लेकिन एक == check fail कर सकती हैं।
Exact, case-sensitive comparison के लिए equals() और जब case मायने न रखे तो equalsIgnoreCase() उपयोग करें।
उदाहरण: Comparing Strings Correctly
public class Main {
public static void main(String[] args) {
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false: different objects
System.out.println(a.equals(b)); // true: same content
}
}
Login to try C/C++/Java/PHP code in the editor
Extracting Substrings
substring(start) और substring(start, end) एक String का एक हिस्सा निकालते हैं — one-argument form start से आखिर तक सब कुछ लेता है, जबकि two-argument form end से बिल्कुल पहले रुकता है (end index exclusive है)।
दोनों Java के String API के बाकी हिस्से की तरह zero-based indices उपयोग करते हैं।
उदाहरण: Extracting Substrings
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "Hello World"
String text = "Hello World";
// Print a line to the console
System.out.println(text.substring(6));
// Print a line to the console
System.out.println(text.substring(0, 5));
}
}
Login to try C/C++/Java/PHP code in the editor
Checking Content Prefixes and Suffixes
startsWith() और endsWith() जांचते हैं कि एक String एक दिए pattern से शुरू या खत्म होता है, filenames को extension से filter करने या एक prefix जांचने के लिए उपयोगी।
contains() ज़्यादा loosely जांचता है, एक substring के string में कहीं भी मौजूद होने के लिए।
उदाहरण: Checking Content Prefixes and Suffixes
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare file and set it to "photo.jpg"
String file = "photo.jpg";
// Print a line to the console
System.out.println(file.startsWith("photo"));
// Print a line to the console
System.out.println(file.endsWith(".jpg"));
System.out.println(file.contains("oto"));
}
}
Login to try C/C++/Java/PHP code in the editor
Trimming and Replacing
trim() leading और trailing whitespace हटाता है (और string के बीच में whitespace को नहीं छूता), जो user input साफ़ करते समय ज़रूरी है जिसमें stray spaces हो सकते हैं।
replace() पूरी string में एक character या substring का हर occurrence किसी दूसरे से बदल देता है।
उदाहरण: Trimming and Replacing
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to " Hello "
String text = " Hello ";
// Print a line to the console
System.out.println(text.trim());
// Print a line to the console
System.out.println("Hello".replace("l", "L"));
}
}
Login to try C/C++/Java/PHP code in the editor
s.trim()याreplace()call करना और result नज़रअंदाज़ करना, क्योंकि strings immutable हैं।equals()याequalsIgnoreCase()के बजाय==उपयोग करना।substring(start, end)उपयोग करना और भूल जाना कि end index exclusive है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: