String Class (non-primitive datatype)

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".
For Example: 
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.
Constructor:
  • 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.
Example:
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
Example:
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.
Example:
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.
Example:
StringBuffer sb = new StringBuffer("Sarthak");
String s = new String(sb);
  • String(StringBuilder s_builder):
    • Allocates a new string from the string in arguments.
Example:
StringBuilder sb1 = new StringBuilder("Sarthak");
String s = new String(sb1);

Methods:
  • int length():
    • Returns the number of characters
Example:
String s = "Sarthak";
System.out.println(s.length());
  • Char charAt(int i):
  • Returns the character at ith index
Example:
System.out.println(s.charAt(3));
  • String substring (int i):
    • Return the substring from the ith  index character to end.
Example:
System.out.println(s.substring(3));
  • String substring (int i, int j):
    • Returns the substring from i to j-1 index.
Example:
System.out.println(s.substring(1, 3));
  • String concat( String str):
    • Concatenates specified string.
Example:
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
Example:
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.
Example:
String s = "I am Student";
int output = s.indexOf("t",7);
  • Int lastIndexOf(String s): 
    • Returns the index within the string of the last occurrence of the specified string.
Example:
String s = "I am Students";
int output = s.lastIndexOf("t");
  • boolean equals( Object otherObj):
    • Compares this string to the specified object.
Example:
System.out.println("Sarthak".equals("Sarthak"));
  • boolean  equalsIgnoreCase (String anotherString): 
    • Compares string to another string with ignoring case.
Example:
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.
Example:
System.out.println("Sarthak".compareTo("Sarthak"));
  • String toLowerCase(): 
    • Converts all the characters in the String to lower case.
Example:
System.out.println("Sarthak".toLowerCase());
  • String toUpperCase(): 
    • Converts all the characters in the String to upper case.
Example:
System.out.println("Sarthak".toUpperCase());
  • String trim(): 
    • Returns the copy of the String, by removing whitespaces at both ends.
Example:
System.out.println(" I am Sarthak ".trim());
  • String replace (char oldChar, char newChar): 
    • Returns new string by replacing all occurrences of oldChar with newChar.
Example:
System.out.println(" I am Sarthak ".replace('a','b'));

How to create String reference?

Differences:

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form