If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Java, the String
class provides a wide range of methods to perform various operations on strings. Here are some common string operations that you can perform using the String
class methods:
Concatenation:
The +
operator is used for string concatenation, which combines two or more strings into a single string.
javaCopy codeString firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // fullName is "John Doe"
Length:
The length()
method returns the number of characters in a string.
javaCopy codeString text = "Hello, World!";
int length = text.length(); // length is 13
Substring:
The substring()
method extracts a portion of the string based on the specified indices.
javaCopy codeString text = "Hello, World!";
String substring1 = text.substring(0, 5); // substring1 is "Hello"
String substring2 = text.substring(7); // substring2 is "World!"
Conversion:
You can convert a string to uppercase or lowercase using the toUpperCase()
and toLowerCase()
methods, respectively.
javaCopy codeString text = "Hello, World!";
String upperCaseText = text.toUpperCase(); // upperCaseText is "HELLO, WORLD!"
String lowerCaseText = text.toLowerCase(); // lowerCaseText is "hello, world!"
Trimming:
The trim()
method removes leading and trailing white spaces from a string.
javaCopy codeString text = " Hello, World! ";
String trimmedText = text.trim(); // trimmedText is "Hello, World!"
Replacement:
The replace()
method replaces occurrences of a substring with another substring.
javaCopy codeString text = "Hello, World!";
String replacedText = text.replace("Hello", "Hi"); // replacedText is "Hi, World!"
Splitting:
The split()
method is used to split a string into an array of substrings based on a delimiter.
javaCopy codeString text = "apple,orange,banana";
String[] fruits = text.split(","); // fruits is {"apple", "orange", "banana"}
Checking Contains:
The contains()
method checks if a particular substring is present in the string.
javaCopy codeString text = "Java Programming";
boolean containsJava = text.contains("Java"); // containsJava is true
boolean containsPython = text.contains("Python"); // containsPython is false
Formatting:
You can format strings using the String.format()
method or by using the +
operator with placeholders.
javaCopy codeString name = "John";
int age = 30;
String formattedString = String.format("My name is %s and I am %d years old.", name, age);
// formattedString is "My name is John and I am 30 years old."
These are just a few examples of the many string operations that you can perform using the String
class methods in Java. Strings are a fundamental part of programming, and understanding how to work with strings is essential in many Java applications.
Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.
Comments: 0