Java Strings
What are Strings?
In the Article, we will Discuss about the Strings of java.
In Java, a String is a sequence of characters. It is a built-in class in the Java language and is commonly used to represent text-based data. Strings are immutable, which means that their values cannot be changed once they are created.
Java Strings:
In Java, strings are represented by the java.lang.String class. A string is a sequence of characters and is used to represent text-based data. Strings are immutable in Java, which means that their values cannot be changed once they are created.
Initializing a String in Java:
Syntax:
String message = "Hello, world!";
Explanation:
In the above example, the String variable message is initialized with the value “Hello, world!” using string literal syntax. String literals are a sequence of characters enclosed in double quotes.
Java provides many built-in methods for working with strings, such as charAt, length, substring, toUpperCase, and toLowerCase, among others.
Some common operations that can be performed on strings in Java:
Example:
String str1 = "Hello"; String str2 = "World"; String result1 = str1 + " " + str2; // "Hello World" String result2 = str1.concat(" ").concat(str2); // "Hello World"
Example:
String str1 = "Hello"; String str2 = "hello"; boolean result = str1.equals(str2); // false
Example:
String str = "Hello"; int length = str.length(); // 5
Example:
String str = "Hello, World!"; String result = str.substring(7, 12); // "World"
Example:
String str = "Hello, World!"; String upperCase = str.toUpperCase(); // "HELLO, WORLD!" String lowerCase = str.toLowerCase(); // "hello, world!"
Java Program to perform Different Operations on a String:
import java.util.*; public class Main{ public static void main(String[] args) { // Initialization of a String String message = "Hello, world!"; System.out.println(message); // returning the First character of the String char firstChar = message.charAt(0); System.out.println(firstChar); // returning the length of the String int messageLength = message.length(); System.out.println(messageLength); // Returns the Substring of the string String substring = message.substring(0, 5); System.out.println(substring); // converting the string to uppercase letters String upperCase = message.toUpperCase(); System.out.println(upperCase); } }
Output:
Hello, world! H 13 Hello HELLO, WORLD!
Explanation:
In this example, the charAt method is used to get the first character of the string, the length method is used to get the length of the string, the substring method is used to get a substring of the string, and the toUpperCase method is used to convert the string to uppercase.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment