Substring
A part of string is called substring. In other words, substring is a subset of another string.
In case of substring startIndex starts from 0 and endIndex starts from 1 or startIndex is inclusive and endIndex is exclusive.
You can get substring from the given String object by one of the two methods:
1. public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
2. public String substring(int startIndex,int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex.
In case of string:
* startIndex:starts from index 0(inclusive).
* endIndex:starts from index 1(exclusive).
class Simple{
public static void main(String args[]){
String s="Anuj Bhargava";
System.out.println(s.substring(5));
System.out.println(s.substring(0,5));
}
}
C:\java>javac simple.java
C:\java>java Simple
Bhargava
Anuj
Methods of String class
java.lang.String class provides a lot of methods to work on string. By the help of these methods, we can perform operations on string such as trimming, concatenating, converting strings etc.
Method | Description |
---|---|
1)public boolean equals(Object anObject) | Compares this string to the specified object. |
2)public boolean equalsIgnoreCase(String another) | Compares this String to another String, ignoring case. |
3)public String concat(String str) | Concatenates the specified string to the end of this string. |
4)public int compareTo(String str) | Compares two strings and returns int |
5)public int compareToIgnoreCase(String str) | Compares two strings, ignoring case differences. |
6)public String substring(int beginIndex) | Returns a new string that is a substring of this string. |
7)public String substring(int beginIndex,int endIndex) | Returns a new string that is a substring of this string. |
8)public String toUpperCase() | Converts all of the characters in this String to upper case |
9)public String toLowerCase() | Converts all of the characters in this String to lower case. |
10)public String trim() | Returns a copy of the string, with leading and trailing whitespace omitted. |
11)public boolean startsWith(String prefix) | Tests if this string starts with the specified prefix. |
12)public boolean endsWith(String suffix) | Tests if this string ends with the specified suffix. |
13)public char charAt(int index) | Returns the char value at the specified index. |
14)public int length() | Returns the length of this string. |
15)public String intern() | Returns a canonical representation for the string object. |
16)public byte[] getBytes() | Converts string into byte array. |
17)public char[] toCharArray() | Converts string into char array. |
18)public static String valueOf(int i) | converts the int into String. |
19)public static String valueOf(long i) | converts the long into String. |
20)public static String valueOf(float i) | converts the float into String. |
21)public static String valueOf(double i) | converts the double into String. |
22)public static String valueOf(boolean i) | converts the boolean into String. |
23)public static String valueOf(char i) | converts the char into String. |
24)public static String valueOf(char[] i) | converts the char array into String. |
25)public static String valueOf(Object obj) | converts the Object into String. |
26)public void replaceAll(String firstString,String secondString) | Changes the firstString with secondString. |