2.String Operation in Java

 

  1. ToUpperCase/ToLowerCase: The toUpperCase method converts all characters in a string to uppercase, while the toLowerCase method converts them all to lowercase.
java
String str = "Hello World"; String upperCaseStr = str.toUpperCase(); 
// upperCaseStr will be "HELLO WORLD" String lowerCaseStr = str.toLowerCase(); // lowerCaseStr will be "hello world"
  1. Contains: The contains method returns a boolean value indicating whether a specified substring occurs within a string.
java
String str = "Hello World"; boolean containsWorld = str.contains("World"); 
// containsWorld will be true boolean containsJava = str.contains("Java");
// containsJava will be false
  1. StartsWith/EndsWith: The startsWith method returns a boolean value indicating whether a string starts with a specified substring, while the endsWith method returns whether it ends with the specified substring.
java
String 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
  1. ValueOf: The valueOf method converts a value of any data type into a string.
java
int num = 123; String strNum = String.valueOf(num); 
// strNum will be "123"


  1. Matches: The matches method tests whether a string matches a specified regular expression.
java
String str = "Hello World"; boolean matches = str.matches(".*World.*"); 
// matches will be true
  1. Substring with two parameters: The substring method can take two parameters to extract a substring between two specified indices.
java
String str = "Hello World"; String substr = str.substring(6, 11); 
// substr will be "World"
  1. compareTo: The compareTo method compares two strings lexicographically, i.e., based on their dictionary order.
java
String str1 = "Hello"; String str2 = "World"; int result = str1.compareTo(str2); 
// result will be negative (-ve) because //"Hello" is less than "World" in //dictionary order
  1. ValueOf with different data types: The valueOf method can be used with different data types to convert them into strings.
java
int 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.

  1. Replace: The replace method replaces all occurrences of a specified character or substring with another character or substring.
java
String str = "Hello World"; String replacedStr = str.replace("o", "0"); 
// replacedStr will be "Hell0 W0rld"
  1. Join: The join method joins multiple strings into a single string, using a specified delimiter.
java
String str1 = "Hello"
String str2 = "World"
String str3 = "Java"
String joinedStr = String.join(", ", str1, str2, str3); 
// joinedStr will be "Hello, World, Java"
  1. Trim: The trim method removes whitespace from the beginning and end of a string.
java
String str = " Hello World "; String trimmedStr = str.trim(); 
// trimmedStr will be "Hello World"
  1. toLowerCase and toUpperCase: The toLowerCase method converts a string to lowercase, while the toUpperCase method converts it to uppercase.
java
String str = "Hello World"; String lowerCaseStr = str.toLowerCase(); 
// lowerCaseStr will be "hello world" String upperCaseStr = str.toUpperCase(); // upperCaseStr will be "HELLO WORLD"
  1. length: The length method returns the length of a string, i.e., the number of characters it contains.
java
String 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

Popular posts from this blog

Cryptography API

Java Applet Overview

Vector in Java