Java Strings
What is a String in Java?
A String in Java is an object that represents a sequence of characters. It is used to store and manipulate text.
Java में String क्या है?
Java में String एक object है जो characters की sequence को represent करता है। इसका उपयोग text को store और manipulate करने के लिए किया जाता है।
Importance of String in Java:
Strings are used everywhere in Java, like for user input, messages, file reading, network communication, etc.
Java में String का महत्व:
Java में Strings हर जगह उपयोग होते हैं जैसे user input, messages, file पढ़ना, network communication आदि।
Common String Functions in Java:
length()
- Get length of stringconcat()
- Concatenate stringsequals()
- Compare two stringstoUpperCase()
,toLowerCase()
- Change casecharAt()
,substring()
- Access parts of string
Java में Common String Functions:
length()
- String की लंबाई निकालेconcat()
- दो Strings जोड़नाequals()
- Strings की तुलना करनाtoUpperCase()
,toLowerCase()
- केस बदलनाcharAt()
,substring()
- String का हिस्सा लेना
Example 1: Declare and Print String
public class StringExample1 {
public static void main(String[] args) {
String message = "Hello, Java!";
System.out.println(message);
}
}
Output: Hello, Java!
Declares a string and prints it.
एक string declare करता है और उसे print करता है।
Example 2: String Length
public class StringExample2 {
public static void main(String[] args) {
String str = "Login Technologies";
System.out.println("Length = " + str.length());
}
}
Output: Length = 18
Uses length() to find string length.
length() से string की लंबाई पता करता है।
Example 3: Concatenate Strings
public class StringExample3 {
public static void main(String[] args) {
String first = "Hello";
String second = "World";
String result = first + " " + second;
System.out.println(result);
}
}
Output: Hello World
Joins two strings using + operator.
+ ऑपरेटर से दो strings जोड़ता है।
Example 4: Compare Strings
public class StringExample4 {
public static void main(String[] args) {
String a = "Java";
String b = "Java";
if(a.equals(b)) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are not equal");
}
}
}
Output: Strings are equal
Compares two strings using equals().
equals() से दो strings की तुलना करता है।
Example 5: Change Case
public class StringExample5 {
public static void main(String[] args) {
String text = "Hello World";
System.out.println(text.toUpperCase());
System.out.println(text.toLowerCase());
}
}
Output:
HELLO WORLD hello world
Converts string to upper and lower case.
String को uppercase और lowercase में बदलता है।