Learn Java Tutorial in Hindi 20 String and its functions with example
This tutorial is made for learning about the String and its functions with example of Java Programming Language in Hindi. The class String includes 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. Case mapping is based on the Unicode Standard version specified by the Character class. STRINGS IN JAVA Java String Class is immutable, i.e. Strings in java, once created and initialized, cannot be changed on the same reference. A java.lang.String class is final which implies no class and extend it. The java.lang.String class differs from other classes, one difference being that the String objects can be used with the += and + operators for concatenation. Two useful methods for String objects are equals( ) and substring( ). The equals( ) method is used for testing whether two Strings contain the same value. The substring( ) method is used to obtain a selected portion of a String. String Methods int codePointAt(int index):It is similar to the charAt method however it returns the Unicode code point value of specified index rather than the character itself. void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin): It copies the characters of src array to the dest array. Only the specified range is being copied(srcBegin to srcEnd) to the dest subarray(starting fromdestBegin). boolean equals(Object obj): Compares the string with the specified string and returns true if both matches else false. boolean contentEquals(StringBuffer sb): It compares the string to the specified string buffer. boolean equalsIgnoreCase(String string): It works same as equals method but it doesn’t consider the case while comparing strings. It does a case insensitive comparison. int compareTo(String string): This method compares the two strings based on the Unicode value of each character in the strings. int compareToIgnoreCase(String string): Same as CompareTo method however it ignores the case during comparison. boolean regionMatches(int srcoffset, String dest, int destoffset, int len): It compares the substring of input to the substring of specified string. substring (starting from the specified offset index) is having the specified prefix or not. boolean startsWith(String prefix): It tests whether the string is having specified prefix, if yes then it returns true else false. int hashCode(): It returns the hash code of the string. int lastIndexOf(int ch): It returns the last occurrence of the character ch in the string. int lastIndexOf(int ch, int fromIndex): Same as lastIndexOf(int ch) method, it starts search from fromIndex. int indexOf(String str): This method returns the index of first occurrence of specified substring str. int lastindexOf(String str): Returns the index of last occurrence of string str. String substring(int beginIndex): It returns the substring of the string. The substring starts with the character at the specified index. String substring(int beginIndex, int endIndex): Returns the substring. The substring starts with character at beginIndex and ends with the character at endIndex. String concat(String str): Concatenates the specified string “str” at the end of the string. String replace(char oldChar, char newChar): It returns the new updated string after changing all the occurrences of oldChar with the newChar. String toLowerCase(Locale locale): It converts the string to lower case string using the rules defined by given locale. String toLowerCase(): Equivalent to toLowerCase(Locale. getDefault()). String toUpperCase(Locale locale): Converts the string to upper case string using the rules defined by specified locale. String toUpperCase(): Equivalent to toUpperCase(Locale.getDefault()). String trim(): Returns the substring after omitting leading and trailing white spaces from the original string. static String copyValueOf(char[] data): It returns a string that contains the characters of the specified character array. static String valueOf(data type): This method returns a string representation of specified data type. int length(): It returns the length of a String.
This tutorial is made for learning about the String and its functions with example of Java Programming Language in Hindi. The class String includes 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. Case mapping is based on the Unicode Standard version specified by the Character class. STRINGS IN JAVA Java String Class is immutable, i.e. Strings in java, once created and initialized, cannot be changed on the same reference. A java.lang.String class is final which implies no class and extend it. The java.lang.String class differs from other classes, one difference being that the String objects can be used with the += and + operators for concatenation. Two useful methods for String objects are equals( ) and substring( ). The equals( ) method is used for testing whether two Strings contain the same value. The substring( ) method is used to obtain a selected portion of a String. String Methods int codePointAt(int index):It is similar to the charAt method however it returns the Unicode code point value of specified index rather than the character itself. void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin): It copies the characters of src array to the dest array. Only the specified range is being copied(srcBegin to srcEnd) to the dest subarray(starting fromdestBegin). boolean equals(Object obj): Compares the string with the specified string and returns true if both matches else false. boolean contentEquals(StringBuffer sb): It compares the string to the specified string buffer. boolean equalsIgnoreCase(String string): It works same as equals method but it doesn’t consider the case while comparing strings. It does a case insensitive comparison. int compareTo(String string): This method compares the two strings based on the Unicode value of each character in the strings. int compareToIgnoreCase(String string): Same as CompareTo method however it ignores the case during comparison. boolean regionMatches(int srcoffset, String dest, int destoffset, int len): It compares the substring of input to the substring of specified string. substring (starting from the specified offset index) is having the specified prefix or not. boolean startsWith(String prefix): It tests whether the string is having specified prefix, if yes then it returns true else false. int hashCode(): It returns the hash code of the string. int lastIndexOf(int ch): It returns the last occurrence of the character ch in the string. int lastIndexOf(int ch, int fromIndex): Same as lastIndexOf(int ch) method, it starts search from fromIndex. int indexOf(String str): This method returns the index of first occurrence of specified substring str. int lastindexOf(String str): Returns the index of last occurrence of string str. String substring(int beginIndex): It returns the substring of the string. The substring starts with the character at the specified index. String substring(int beginIndex, int endIndex): Returns the substring. The substring starts with character at beginIndex and ends with the character at endIndex. String concat(String str): Concatenates the specified string “str” at the end of the string. String replace(char oldChar, char newChar): It returns the new updated string after changing all the occurrences of oldChar with the newChar. String toLowerCase(Locale locale): It converts the string to lower case string using the rules defined by given locale. String toLowerCase(): Equivalent to toLowerCase(Locale. getDefault()). String toUpperCase(Locale locale): Converts the string to upper case string using the rules defined by specified locale. String toUpperCase(): Equivalent to toUpperCase(Locale.getDefault()). String trim(): Returns the substring after omitting leading and trailing white spaces from the original string. static String copyValueOf(char[] data): It returns a string that contains the characters of the specified character array. static String valueOf(data type): This method returns a string representation of specified data type. int length(): It returns the length of a String.