STRING FUNCTIONS/METHODS: A number of methods provided in Java to perform operations in Strings are called String functions. A string is an object that represents a sequence of characters or char values. The java. lang. A string class is used to create a Java string object. There are so many methods available which we will discuss in this blog. Strings in Java are constant, and it is created either using a literal or using a keyword. String literal is used to make Java memory efficient, and the keyword creates a Java string in normal memory. The string represents an array of character values, and the class is implemented by three interfaces Serializable, Comparable, and Char Sequence interfaces. It represents the sequence of characters in a serialized or comparable manner.
We are using string functions to perform the string-related validations in java.
Main Concept of string functions in Java
Below are the main concepts of String Functions in java:
1. Creating String
2. String length
3. Concatenating string
4. Creating a format string
1. Creating String: There are two ways in which a String object can be created in Java:
Using a string literal: A string literal in Java can be created using double quotes. Ex. String s= "Hello NumPy Ninja!";
Using the new keyword: Java String can be created by using the keyword “new”. Ex. String s= new String("Hello NumPy Ninja!");
2. String length: Methods that are used to get information about an object are called accessor methods in Java. One such accessor method related to strings is the length () method. length(): we are using this function to know the length of the string.
Syntax: variable. Length();
class Main {
public static void main(String[] args) {
String str1 = "Java is fun";
System.out.println(str1.length());
}
}
// Output: 11
3. Concatenating string: This method returns a new string which is string1 with string2 combined at the end. Concat() method can be used with string literals to get this done. Strings are also commonly concatenated using the + operator.
public class ExerciseNew {
public static void main(String args[]){
String s1="Hello,";
s1=s1.concat("What is your good name?");
System.out.println(s1);
}}
Output: Hello,What is your good name?
4. Creating a format string: We have printf () and format () methods that prints output with formatted numbers. There is an equivalent class method in String called format (). It returns a String object. The static format () method available in the String object allows the creation of a formatted string that can be reused, contrary to the one-time print statement.
The format() method returns a formatted string based on the argument passed. Ex.
class Main {
public static void main(String[] args) {
String str = "Java";
// format string
String formatStr = String.format("Language: %s", str);
System.out.println(formatStr);
}
}
// Output: Language: Java
Methods of String functions in Java
The following are the different methods:
startsWith & endsWith validation:
startsWith: To verify whether the string is starting with a specific character or not. Syntax: variable. startsWith (“expected text”)
class Main {
public static void main(String[] args) {
String str = "JavaScript";
// checks if "JavaScript" starts with "Java"
System.out.println(str.startsWith("Java"));
}
}
// Output: true
endsWith: To verify whether the variable is ending with a specific character or notSyntax: variable. endsWith (“expected text”)
class Main {
public static void main(String[] args) {
String str = "Java Programming";
System.out.println(str.endsWith("mming")); // true
System.out.println(str.endsWith("g")); // true
System.out.println(str.endsWith("a Programming")); // true
System.out.println(str.endsWith("programming")); // false
System.out.println(str.endsWith("Java")); // false
}
}
toLowerCase() & toUpperCase():
toLowerCase(): To convert string from uppercase into lowercase
Syntax: variable. toLowerCase ();
toUpperCase: To convert string from lowercase into uppercase
Syntax: variable. toUpperCase ();
Equals() & equalsIgnoreCase():
Equals(): To verify whether one string is equal to another string or not.
Syntax: variable. equals (second/another variable)
equalsIgnoreCase(): To verify whether a variable is the same as another variable or not irrespective of case.
Syntax: variable. equalsIgnoreCase (another variable)
Contains(): To verify whether the string contains a specific value or string or not.
Syntax: variable. contains(“value”);
toCharArray(): The Java String toCharArray() method converts the string to a char array and returns it.
Syntax: string.toCharArray()
Ex: class Main {
public static void main(String[] args) {
String str = "Java Programming";
// creating a char array
char[] result;
result = str.toCharArray();
System.out.println(result); // Java Programming
}
}
charAt(): To verify which character is available in which index of the string
Syntax: variable. charAT (index no);
Note: Usually Length starts with 1 and Index in the string starts with 0
Ex.:
public class CharAtExample{
public static void main(String args[]){
String name="javatpoint";
char ch=name.charAt(4);//returns the char value at the 4th index
System.out.println(ch);
}}
Output: t
indexOf(): To verify in which Index which character is available.
Syntax: variable.indexOf (char);
Trim(): We are using this function to eliminate starting and ending spaces of the string (first and last spaces).
Syntax: variable. trim ()
public class StringTrimExample{
public static void main(String args[]){
String s1=" hello string ";
System.out.println(s1+"javatpoint");//without trim()
System.out.println(s1.trim()+"javatpoint");//with trim()
}}
Output
hello string javatpoint
hello stringjavatpoint
replace(): To replace old characters or Strings with new characters or strings in a Var.
Syntax: var. replace (“Old character/String”, “New Character/String”);
Split(): To split variable/String into multiple substrings
Syntax: variable. Split (“separator”);
Note: All split values will be stored in the array. That is the reason the return type for the split function is String [].
Example: Prepare a program to split a variable into multiple substrings and print all strings one by one
1. isEmpty(): isEmpty() method checks if the input string is empty or not. Note that here empty means the number of characters contained in a string is zero. public boolean isEmpty()
public class IsEmptyExample{
public static void main(String args[]){
String s1="";
String s2="javatpoint";
System.out.println(s1.isEmpty());
System.out.println(s2.isEmpty());
}}
Output:
True
false
String valueOf(): valueOf() method converts different types of values into strings. With the help of the string valueOf() method, you can convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to a string, and char array to string. Example
public class StringValueOfExample{
public static void main(String args[]){
int value=30;
String s1=String.valueOf(value);
System.out.println(s1+10);//concatenating string with 10
}}
Output: 3010
Hope you got a good idea about string functions/methods in java. Thank you for reading.
Happy Testing!!