2.String Operation in Java
- ToUpperCase/ToLowerCase: The toUpperCasemethod converts all characters in a string to uppercase, while thetoLowerCasemethod converts them all to lowercase.
javaString str = "Hello World";
String upperCaseStr = str.toUpperCase(); // upperCaseStr will be "HELLO WORLD"
String lowerCaseStr = str.toLowerCase(); // lowerCaseStr will be "hello world"
- Contains: The containsmethod returns a boolean value indicating whether a specified substring occurs within a string.
javaString str = "Hello World";
boolean containsWorld = str.contains("World"); // containsWorld will be true
boolean containsJava = str.contains("Java");// containsJava will be false
- StartsWith/EndsWith: The startsWithmethod returns a boolean value indicating whether a string starts with a specified substring, while theendsWithmethod returns whether it ends with the specified substring.
javaString str = "Hello World";boolean startsWithHello = str.startsWith("Hello");// startsWithHello will be true
boolean endsWithWorld = str.endsWith("World"); // endsWithWorld will be true
boolean endsWithJava = str.endsWith("Java"); // endsWithJava will be false
- ValueOf: The valueOfmethod converts a value of any data type into a string.
javaint num = 123;
String strNum = String.valueOf(num); // strNum will be "123"
- Matches: The matchesmethod tests whether a string matches a specified regular expression.
javaString str = "Hello World";
boolean matches = str.matches(".*World.*"); // matches will be true
- Substring with two parameters: The substringmethod can take two parameters to extract a substring between two specified indices.
javaString str = "Hello World";
String substr = str.substring(6, 11); // substr will be "World"
- compareTo: The compareTomethod compares two strings lexicographically, i.e., based on their dictionary order.
javaString str1 = "Hello";
String str2 = "World";
int result = str1.compareTo(str2); // result will be negative (-ve) because //"Hello" is less than "World" in //dictionary order
- ValueOf with different data types: The valueOfmethod can be used with different data types to convert them into strings.
javaint num = 123;
double dbl = 3.14;
char ch = 'A';
boolean bool = true;
String strNum = String.valueOf(num); // strNum will be "123" String strDbl = String.valueOf(dbl); // strDbl will be "3.14" String strCh = String.valueOf(ch);// strCh will be "A" String strBool = String.valueOf(bool); // strBool will be "true"
These are just a few more examples of string operations in Java. With these and other string methods available in Java, you can perform a wide range of string manipulation and processing tasks in your programs.
- Replace: The replacemethod replaces all occurrences of a specified character or substring with another character or substring.
javaString str = "Hello World";
String replacedStr = str.replace("o", "0"); // replacedStr will be "Hell0 W0rld"
- Join: The joinmethod joins multiple strings into a single string, using a specified delimiter.
javaString str1 = "Hello"; String str2 = "World"; String str3 = "Java"; String joinedStr = String.join(", ", str1, str2, str3); // joinedStr will be "Hello, World, Java"
- Trim: The trimmethod removes whitespace from the beginning and end of a string.
javaString str = "    Hello World   ";
String trimmedStr = str.trim(); // trimmedStr will be "Hello World"
- toLowerCase and toUpperCase: The toLowerCasemethod converts a string to lowercase, while thetoUpperCasemethod converts it to uppercase.
javaString str = "Hello World";
String lowerCaseStr = str.toLowerCase(); // lowerCaseStr will be "hello world"
String upperCaseStr = str.toUpperCase(); // upperCaseStr will be "HELLO WORLD"
- length: The lengthmethod returns the length of a string, i.e., the number of characters it contains.
javaString str = "Hello World"; int len = str.length(); // len will be 11
These are just a few more examples of string operations in Java. With these and other string methods available in Java, you can perform a wide range of string manipulation and processing tasks in your programs.
Comments
Post a Comment