Sunday 12 June 2016

String Class in Java

In this blog post we will learn about String Class in Java.Before proceeding to String Class we should learn what is the means of String in java programming language.




String:
"String are a sequence of characters.For performing any operations on String we need string object which comes from String Class.This class derived from java.lang package and Implemented Interfaces are Serializable, CharSequence, Comparable<String>.Every class in java directly or indirectly derived from object class." 

String is a final class and class declaration structure are given below:

public final class String extends Object implements Serializable, Comparable<String>, CharSequence{


}


Strings are constant,their values cannot be changed after they are created or assigned.String buffers & String Builder support mutable strings.Because String objects are immutable,so it's easy to share it across different threads or functions.We can instantiate String by two ways.

Demo Example:

First method:
        String str1 = "Navneet";

Second method:
   String str2 = new String (“Navneet”);


The String class includes various methods for examining individual characters of the sequence,for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters ,translated to uppercase or to lowercase.

The Java language provides special support for the string concatenation operator symbol ( + ), and for conversion of other objects to strings.String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method.String conversions are implemented through the "toString" method,derived by Object class and inherited by all classes in Java.

The Below is the various methods supported by String Class.

1- charAt (int index)
Returns the char value at the specified index.
2-codePointAt(int index)
Returns the character (Unicode code point) at the specified index.
3-codePointBefore(int index)
Returns the character (Unicode code point) before the specified index.
4-codePointCount(int beginIndex, int endIndex)
Returns the number of Unicode code points in the specified text range of this String.
5-compareTo(String anotherString)
Compares two strings lexicographically.
6-compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences.
7-concat(String str)
Concatenates the specified string to the end of this string.
8-contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
9-contentEquals(CharSequence cs)
Compares this string to the specified CharSequence.
10-contentEquals(StringBuffer sb)
Compares this string to the specified StringBuffer.
11-copyValueOf(char[] data)
Equivalent to valueOf(char[]).
12-copyValueOf(char[] data, int offset, int count)
13-valueOf(char[], int, int).
14-endsWith(String suffix)
Tests if this string ends with the specified suffix.
15-equals(Object anObject)
Compares this string to the specified object.
16-equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations.
17-format(Locale l, String format, Object... args)
Returns a formatted string using the specified locale, format string, and arguments.
18-format(String format, Object... args)
Returns a formatted string using the specified format string and arguments.
19-getBytes()
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
20-getBytes(Charset charset)
Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
21-getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
Deprecated.
This method does not properly convert characters into bytes.
22-getBytes(String charsetName)
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
23-getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.
24-hashCode()
Returns a hash code for this string.
25-indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
26-indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
27-indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
28-indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
29-intern()
Returns a canonical representation for the string object.
30-isEmpty()
Returns true if, and only if, length() is 0.
31-join(CharSequence delimiter, CharSequence... elements)
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
32-join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
33-lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
34-lastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
35-lastIndexOf(String str)
Returns the index within this string of the last occurrence of the specified substring.
36-lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
37-length()
Returns the length of this string.
38-matches(String regex)
Tells whether or not this string matches the given regular expression.
39-offsetByCodePoints(int index, int codePointOffset)
Returns the index within this String that is offset from the given index by codePointOffset code points.
40-regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
41-regionMatches(int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
42-replace(char oldChar, char newChar)
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
43-replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
44-replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
45-replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.
46-split(String regex)
Splits this string around matches of the given regular expression.
47-split(String regex, int limit)
Splits this string around matches of the given regular expression.
48-startsWith(String prefix)
Tests if this string starts with the specified prefix.
49-startsWith(String prefix, int toffset)
Tests if the substring of this string beginning at the specified index starts with the specified prefix.
50-subSequence(int beginIndex, int endIndex)
Returns a character sequence that is a subsequence of this sequence.
51-substring(int beginIndex)
Returns a string that is a substring of this string.
52-substring(int beginIndex, int endIndex)
Returns a string that is a substring of this string.
53-toCharArray()
Converts this string to a new character array.
54-toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale.
55-toLowerCase(Locale locale)
Converts all of the characters in this String to lower case using the rules of the given Locale.
56-toString()
This object (which is already a string!) is itself returned.
57-toUpperCase()
Converts all of the characters in this String to upper case using the rules of the default locale.
58-toUpperCase(Locale locale)
Converts all of the characters in this String to upper case using the rules of the given Locale.
59-trim()
Returns a string whose value is this string, with any leading and trailing whitespace removed.

The various polymorphic forms of valueOf methods are....
60-valueOf(boolean b)
Returns the string representation of the boolean argument.
61-valueOf(char c)
Returns the string representation of the char argument.
62-valueOf(char[] data)
Returns the string representation of the char array argument.
63-valueOf(char[] data, int offset, int count)
Returns the string representation of a specific subarray of the char array argument.
64-valueOf(double d)
Returns the string representation of the double argument.
65-valueOf(float f)
Returns the string representation of the float argument.
66-valueOf(int i)
Returns the string representation of the int argument.
67-valueOf(long l)
Returns the string representation of the long argument.
68-valueOf(Object obj)
Returns the string representation of the Object argument.

No comments:

Post a Comment