String Class:
Syntax:
public final class String
extends Object
implements Serializable, Comparable
Use:
- The String class represents character strings.
- All string literals in Java programs, such as "abc".
String str="sarthak";
or
char name[] = {'s', 'a', 'r','t', 'h', 'a','k'};
String str = new String(name);
- In java, objects of String are immutable.
- It cannot be changed once created.
- The java.lang.String class provides a lot of methods to work on string.
- we can perform operations on string such as trimming, concatenating, converting, comparing, replacing strings etc.
- String(byte[] byte_arr):
- Construct a new String by decoding the byte array.
Example:
byte[] b = {65, 66, 67};
String s =new String(b);
- String(byte[] byte_arr, int start_index, int length)
- Construct a new string from the bytes array depending on the arguments.
byte[] b1 = {65, 66, 67, 68, 69};
String s = new String(b1, 1, 3);
- String(char[] char_arr)
- Allocates a new String from the given Character array
char name[] = {'s', 'a', 'r','t', 'h', 'a','k'};
String s = new String(name);
- String(char[] char_array, int start_index, int count)
- Allocates a String from a given character array as per arguments.
char name[] = {'s', 'a', 'r','t', 'h', 'a','k'};
String sn = new String(name, 1, 3);
- String(StringBuffer s_buffer):
- Allocates a new string from the string in argument.
StringBuffer sb = new StringBuffer("Sarthak");
String s = new String(sb);
- String(StringBuilder s_builder):
- Allocates a new string from the string in arguments.
StringBuilder sb1 = new StringBuilder("Sarthak");
String s = new String(sb1);
- int length():
- Returns the number of characters
String s = "Sarthak";
System.out.println(s.length());
- Char charAt(int i):
- Returns the character at ith index
System.out.println(s.charAt(3));
- String substring (int i):
- Return the substring from the ith index character to end.
System.out.println(s.substring(3));
- String substring (int i, int j):
- Returns the substring from i to j-1 index.
System.out.println(s.substring(1, 3));
- String concat( String str):
- Concatenates specified string.
String s1="Sarthak";
String s2="Patel";
System.out.println(s1.concat(s2));
- int indexOf (String s):
- Returns the index within the string of the first occurrence
String s = "I am Student";
int output = s.indexOf("I");
- int indexOf (String s, int i):
- Returns the index within the string of the first occurrence of the specified string as per argument.
String s = "I am Student";
int output = s.indexOf("t",7);
String s = "I am Students";
int output = s.lastIndexOf("t");
System.out.println("Sarthak".equals("Sarthak"));
System.out.println("Sarthak".equalsIgnoreCase("Sarthak"));
System.out.println("Sarthak".compareTo("Sarthak"));
System.out.println("Sarthak".toLowerCase());
System.out.println("Sarthak".toUpperCase());
System.out.println(" I am Sarthak ".trim());
System.out.println(" I am Sarthak ".replace('a','b'));
- Int lastIndexOf(String s):
- Returns the index within the string of the last occurrence of the specified string.
String s = "I am Students";
int output = s.lastIndexOf("t");
- boolean equals( Object otherObj):
- Compares this string to the specified object.
System.out.println("Sarthak".equals("Sarthak"));
- boolean equalsIgnoreCase (String anotherString):
- Compares string to another string with ignoring case.
System.out.println("Sarthak".equalsIgnoreCase("Sarthak"));
- int compareTo( String anotherString):
- Compares two string lexicographically.
- It will return three values:
- out < 0 // s1 comes before s2
- out = 0 // s1 and s2 are equal.
- out > 0 // s1 comes after s2.
System.out.println("Sarthak".compareTo("Sarthak"));
- String toLowerCase():
- Converts all the characters in the String to lower case.
System.out.println("Sarthak".toLowerCase());
- String toUpperCase():
- Converts all the characters in the String to upper case.
System.out.println("Sarthak".toUpperCase());
- String trim():
- Returns the copy of the String, by removing whitespaces at both ends.
System.out.println(" I am Sarthak ".trim());
- String replace (char oldChar, char newChar):
- Returns new string by replacing all occurrences of oldChar with newChar.
System.out.println(" I am Sarthak ".replace('a','b'));
Tags:
Core java