← Back to Core Java Course | Chapter 6: Strings | Lesson 2 of 5

Java String के Methods

String methods text के लिए ready-made tools हैं, जैसे letters बड़े बनाना, pieces काटना, या जांचना कि एक word कैसे शुरू होता है। वे पुराने text बदलने के बजाय नया text वापस देते हैं।
Syntax
java
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

java
// 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());
	}
}

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

java
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
	}
}

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

java
// 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));
	}
}

Checking Content Prefixes and Suffixes

startsWith() और endsWith() जांचते हैं कि एक String एक दिए pattern से शुरू या खत्म होता है, filenames को extension से filter करने या एक prefix जांचने के लिए उपयोगी।

contains() ज़्यादा loosely जांचता है, एक substring के string में कहीं भी मौजूद होने के लिए।

उदाहरण: Checking Content Prefixes and Suffixes

java
// 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"));
	}
}

Trimming and Replacing

trim() leading और trailing whitespace हटाता है (और string के बीच में whitespace को नहीं छूता), जो user input साफ़ करते समय ज़रूरी है जिसमें stray spaces हो सकते हैं।

replace() पूरी string में एक character या substring का हर occurrence किसी दूसरे से बदल देता है।

उदाहरण: Trimming and Replacing

java
// 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"));
	}
}
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. s.trim() या replace() call करना और result नज़रअंदाज़ करना, क्योंकि strings immutable हैं।
  2. equals() या equalsIgnoreCase() के बजाय == उपयोग करना।
  3. substring(start, end) उपयोग करना और भूल जाना कि end index exclusive है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.