Saturday, January 11, 2020

String

Strings in Java


 A String is a standard object in Java of Class String and is provided built-in support. The class String is a final class(means it can not be inherited) and the objects of this class are immutable(means these are read-only). The String object is provided support for +,+=(concatenation) operators. The String class objects are thread-safe. We can use StringBuffer and StringBuilder classes for using a mutable string.


String class has two constructors


public String()

public String(String value)

Declaring and Creating a String Object


String str1=new String("Welcome");
String str2="strings";

char c[]= {'w','e','l','c','o','m','e'};

String s=new String(c);

String Constant Pool


In Java, when a String variable is created by simply assigning the value to the string object, an object is first searched in the string constant pool, if it is not found there then the only a new object will be created, otherwise, the existing object will be referred, but if the new object is created with the "new" keyword which will always create a new object in memory. 

String constant pool
String


Searching some "char" or "string" in Strings


Several overloads of indexOf() search forward and lastIndexOf() search backward methods are available for searching in the String class. Every method returns the index of what is searched or -1 if it is not found.

String Methods


  • The charAt() method returns the character at the provided index (position). The return type of this method is char.
  • The codePointAt() method returns the Unicode of the character at the provided index. The return type of this method is int.
  • The codePointBefore() method returns the Unicode of the character before the provided index. The return type of this method is int.
  • The codePointCount() method returns the Unicode in the provided text range of this String. The return type of this method is int.
  • The compareTo() method compares two strings in the dictionary order. The return type of this method is int.
  • The compareToIgnoreCase() method compares two strings dictionary order, ignoring the alphabet case. The return type of this method is int.
  • The concat() method appends a string at the end of another string. The return type of this method is String.
  • The contains() method tests if a string contains a sequence of characters. The return type of this method is boolean
  • The contentEquals() method tests if a string contains the exact similar sequence of characters of the provided CharSequence or StringBuffer. The return type of this method is boolean.
  • The copyValueOf() method returns a string by converting the chars of a character array. The return type of this method is String.
  • The endsWith() method tests if a string ends with the provided character/s. The return type of this method is Boolean.
  • The equals() method compares two strings. The equals() method returns true if the strings are equal, and false if not. The return type of this method is boolean.
  • The equalsIgnoreCase() method compares two strings, ignoring alphabet cases. The return type of this method is Boolean.
  • The format() method returns a formatted string using the provided locale, format string, and arguments. The return type of this method is String.
  • The getBytes() method encodes a String into a sequence of bytes, It saves the result into a new array of bytes. The return type of this method is byte[].
  • The getChars() method copies chars from a string to an array of chars. The return type of this method is void.
  • The hashCode() method is used to find the hash code of a string. The return type of this method is int.
  • The indexOf() method returns the position of the first occurrence of provided chars in a string. The return type of this method is int.
  • The intern() method returns the index within this string of the first occurrence of the provided character, starting the search at the provided index. The return type of this method is String.
  • The isEmpty() method tests if a string is empty or not. The return type of this method is Boolean.
  • The lastIndexOf() method returns the position of the last found occurrence of provided characters in a string. The return type of this method is int.
  • The length() finds the length of a provided string. The return type of this method is int.
  • The matches() finds a string for a match against a regular expression, and the method returns the matches. The return type of this method is Boolean.
  • The offsetByCodePoints() method returns the index within this String that is offset from the given index by codePointOffset code points. The return type of this method is int.
  • The regionMatches() method checks if two string regions are equal. The return type of this method is boolean.
  • The replace() method searches a string for a provided value, and the method returns a new string where the provided values are replaced. The return type of this method is String.
  • The replaceFirst() method replaces the first occurrence of a substring that matches the given regular expression with the given replacement. The return type of this method is String.
  • The replaceAll() method replaces each substring of this string that matches the given regular expression with the given replacement. The return type of this method is String.
  • The split() method can be used to split an input string into an array of substrings. The return type of this method is String[].
  • The startsWith() method tests that a string starts with provided characters. The return type of this method is boolean.
  • The subSequence() method can be used to get a new character sequence which is the subsequence of the input sequence. The return type of this method is CharSequence.
  • The substring() method fetches the characters from a string, beginning at a provided start position, and through the provided number of character. The return type of this method is String.
  • The toCharArray() method can be used to change the input string into a new character array. The return type of this method is char[].
  • The toLowerCase() method can be used to change a string to lower case alphabets. The return type of this method is String.
  • The toUpperCase() method can be used to change a string to upper case alphabets. The return type of this method is String.
  • The trim() method compares whitespace from both ends of a string. The return type of this method is String.
  • The valueOf() method is used to get the primitive value of a String object. The return type of this method is String.

Video Tutorial