String is a pre-define class in java.lang package. String object is immutable. Array of character/group of character/sequence of character are a string. This string is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. String is a pre-define class in java.lang package.In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
char s[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization, then you can write the above statement as follows:
char s[] = "Hello";
Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object.
The following program is an example of length(), method String class.
Here is the list of methods supported by String class −
String Methods
1. String length(string)
Returns the length of this string.
Example -
class A{
public static void main(String []arr){
String s="Hello";
System.out.print(s.length());
}
}
Output -
5
2. String charAt(int index)
Returns the character at the specified index.Example -
class A{
public static void main(String []arr){
String s="Hello";
System.out.print(s.charAt(2));
}
}
Output -
l
3. Int compareTo(string)
Compares this String to another string.Example -
class A{
public static void main(String []arr){
String s="Hello";
int i=s.compareTo("hello");
System.out.print(i);
}
}
Output -
-32
4. Int compareToIgnoreCase(string)
Compares two strings lexicographically, ignoring case differences.Example -
class A{
public static void main(String []arr){
String s="Hello";
int i=s.compareToIgnoreCase("hello");
System.out.print(i);
}
}
Output -
0
5. String concat(string)
Concatenates the specified string to the end of this string.Example -
class A{
public static void main(String []arr){
String s="Hello";
s=s.concat(" Bye");
System.out.print(s);
}
}
Output -
Hello Bye
6. String equals(string)
Compares this string to the specified object.Example -
class A{
public static void main(String []arr){
String s="Hello";
System.out.print(s.equals("hello"));
}
}
Output -
false
Example -
class A{
public static void main(String []arr){
String s="Hello";
boolean b=s.equals("Hello");
System.out.print(b);
}
}
Output -
true
7. String equalsIgnoreCase(string)
Compares this String to another String, ignoring case considerations.
Example -
class A{
public static void main(String []arr){
String s="Hello";
System.out.print(s.equalsIgnoreCase("hElLo"));
}
}
Output -
true
8. char to CharArray()
Converts this string to a new character array.Example -
class A{
public static void main(String []arr){
String s="Hello";
char c[]=s.toCharArray();
for(int i=0; i<c.length; i++){
System.out.print(c[i]);
}
}
}
Output -
Hello
9. String endsWith(string)
Tests if this string ends with the specified suffix.Example -
class A{
public static void main(String []arr){
String s="Hello friends";
System.out.print(s.endsWith("ds"));
}
}
Output -
true
10. String startWith(string)
Tests if this string starts with the specified prefix.Example -
class A{
public static void main(String []arr){
String s="Hello friends";
System.out.print(s.startsWith("ell"));
}
}
Output -
false
11. String subString(int)
Returns a new string that is a substring of this string.Example -
class A{
public static void main(String []arr){
String s="Hello friends";
s=s.substring(2,9);
System.out.print(s);
}
}
Output -
llo fri
12. Int indexOf(string)
Returns the index within this string of the first occurrence of the specified substring.Example -
class A{
public static void main(String []arr){
String s="Hello friends";
int i=s.indexOf("en");
System.out.print(i);
}
}
Output -
9
class A{
public static void main(String []arr){
String s="Hello friends";
int i=s.indexOf("en",2);
System.out.print(i);
}
}
Output -
9
13. Int lastIndexOf(string)
Returns the index within this string of the rightmost occurrence of the specified substring.Example -
class A{
public static void main(String []arr){
String s="Hello friends";
int i=s.lastIndexOf("e",5);
System.out.print(i);
}
}
Output -
1
14. String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.Example -
class A{
public static void main(String []arr){
String s=" Hello friends ";
s=s.trim();
System.out.print(s);
}
}
Output -
Hello friends
15. String valueOf(int)
Returns the string representation of the passed data type argument.Example -
class A{
public static void main(String []arr){
int i=5;
String s=String.valueOf(i);
System.out.print(s);
}
}
Output -
5
16. String toUpperCase(string)
Converts all of the characters in this String to upper case using the rules of the given Locale.Example -
class A{
public static void main(String []arr){
String s="Hello Friends";
s=s.toUpperCase();
System.out.print(s);
}
}
Output -
HELLO FRIENDS
17. String toLowerCase(string)
Converts all of the characters in this String to lower case using the rules of the given Locale.Example -
class A{
public static void main(String []arr){
String s="Hello Friends";
s=s.toUpperCase();
System.out.print(s);
}
}
Output -
hello friends
18. String split()
Splits this string around matches of the given regular expression.Example -
class A{
public static void main(String []arr){
String s="Hello Friends";
String t[]=s.split("e");
for(int i=0; i<t.length; i++){
System.out.print(t[i]);
}
}
}
Output -
H
llo frinds
0 Comments