String class provides various methods to perform some operations on string class like concatenation of string, removing white spaces from string, converting string to upper case, converting string to lower case, checking strings equality, converting string in character array, converting other datatype into string etc and so on.
In below program get each and every method of String class with detailed description of how to use and where to use in programs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import java.nio.charset.Charset; import java.util.Locale; public class StringClassMethods { public static void main(String[] args) { String s1 = "Java-Identifiers"; String s2 = new String("Java-Identifiers"); String s3 = "java-identificatori"; //This method - Returns the char value at the specified index. System.out.println("charAt() : "+s1.charAt(5)); //This method - Returns the character (Unicode code point) at the specified index. System.out.println("codePointAt() : "+s1.codePointAt(5)); //This method - Returns the character (Unicode code point) before the specified index. System.out.println("codePointBefore() : "+s1.codePointBefore(5)); //This method - Returns the number of Unicode code points in the specified text range of this String. System.out.println("codePointCount() : "+s1.codePointCount(3, 5)); //This method - Compares two strings lexicographically. System.out.println("compareTo() : "+s1.compareTo("Java-Identifiers")); //This method - Compares two strings lexicographically, ignoring case differences. System.out.println("compareToIgnoreCase() : "+s1.compareToIgnoreCase("JAVA-IDENTIFIERS")); //This method - Concatenates the specified string to the end of this string. System.out.println("concat() : "+s1.concat(".com")); //This method - Returns true if and only if this string contains the specified sequence of char values. System.out.println("contains() : "+s1.contains("Java")); //This method - Compares this string to the specified CharSequence. System.out.println("contentEquals(CharSequence c) : "+s1.contentEquals("Java-Identifiers")); //This method - Compares this string to the specified CharSequence. System.out.println("contentEquals(StringBuffer sb) : "+s1.contentEquals(new StringBuffer("Java-Identifiers"))); //This method - Returns a String that represents the character sequence in the array specified. System.out.println("copyValueOf() : "+String.copyValueOf(s1.toCharArray())); //This method - Returns a String that represents the character sequence in the array specified. System.out.println("copyValueOf(char[] data, int offset, int count) : "+String.copyValueOf(s1.toCharArray(),0,4)); //This method - Tests if this string ends with the specified suffix. System.out.println("endsWith() : "+s1.endsWith(".com")); //This method - Compares this string to the specified object. System.out.println("equals() : "+s1.equals("Java-Identifiers")); //This method - Compares this String to another String, ignoring case considerations. System.out.println("equalsIgnoreCase() : "+s1.equalsIgnoreCase("JAVA-IDENTIFIERS")); //This method - Encodes this String into a sequence of bytes System.out.println("getBytes() : "+s1.getBytes()); //This method - Encodes this String into a sequence of bytes using the given charset System.out.println("getBytes(Charset c) : "+s1.getBytes(Charset.defaultCharset())); //This method - Copies characters from this string into the destination character array. System.out.print("getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) : "); char[] arr = new char[5]; s1.getChars(0, 4, arr, 0); for(char c : arr) { System.out.print(c); } //This method - Returns a hash code for this string. System.out.println("\nhashCode() : "+s1.hashCode()); //This method - Returns the index within this string of the first occurrence of the specified character. System.out.println("indexOf(int ch) : "+s1.indexOf("I")); //This method - Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index. System.out.println("indexOf(int ch, int fromIndex) : "+s1.indexOf("I",0)); //This method - Returns the index within this string of the first occurrence of the specified substring. System.out.println("indexOf(String str) : "+s1.indexOf("Ident")); //This method - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. System.out.println("indexOf(String str,int fromIndex) : "+s1.indexOf("Ident",0)); //This method - Returns a canonical representation for the string object. System.out.println("intern() : "+s2.intern()); //This method - Returns true if, and only if, length() is 0. System.out.println("isEmpty() : "+s1.isEmpty()); //This method - Returns the index within this string of the last occurrence of the specified character. System.out.println("lastIndexOf(int ch) : "+s1.lastIndexOf("e")); //This method - Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. System.out.println("lastIndexOf(int ch, int fromIndex) : "+s1.lastIndexOf("e",13)); //This method - Returns the index within this string of the last occurrence of the specified substring. System.out.println("lastIndexOf(String str) : "+s1.lastIndexOf("Ident")); //This method - Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. System.out.println("lastIndexOf(String str, int fromIndex) : "+s1.lastIndexOf("Ident",0)); //This method - Returns the length of this string. System.out.println("length() : "+s1.length()); //This method - Tells whether or not this string matches the given regular expression. System.out.println("matches() : "+s1.matches("[a-zA-Z]")); //This method - Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. System.out.println("replace(char oldChar, char newChar) : "+s1.replace("Java", "Pushkar")); //This method - Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. System.out.println("replace(CharSequence target, CharSequence replacement) : "+s1.replace(s1, "Pushkar")); //This method - Replaces each substring of this string that matches the given regular expression with the given replacement. System.out.println("replaceAll(String regex, String replacement) : "+s1.replaceAll("[-]", "")); //This method - Replaces the first substring of this string that matches the given regular expression with the given replacement. System.out.println("replaceFirst(String regex, String replacement) : "+s1.replaceFirst("a", "")); //This method - Splits this string around matches of the given regular expression. System.out.println("split(String regex) : "+s1.split("-")[0]); //This method - Splits this string around matches of the given regular expression. System.out.println("split(String regex, int limit) : "+s1.split("-",0)[0]); //This method - Tests if this string starts with the specified prefix. System.out.println("startsWith(String prefix) : "+s1.startsWith("Java")); //This method - Tests if the substring of this string beginning at the specified index starts with the specified prefix. System.out.println("startsWith(String prefix, int toffset) : "+s1.startsWith("Java",0)); //This method - Returns a new character sequence that is a subsequence of this sequence. System.out.println("subSequence(int beginIndex, int endIndex) : "+s1.subSequence(5, 8)); //This method - Returns a new string that is a substring of this string. System.out.println("substring(int beginIndex) : "+s1.substring(4)); //This method - Returns a new string that is a substring of this string. System.out.println("substring(int beginIndex, int endIndex) : "+s1.substring(4,10)); //This method - Converts this string to a new character array. System.out.println("toCharArray() : "+s1.toCharArray()); //This method - Converts all of the characters in this String to lower case System.out.println("toLowerCase() : "+s1.toLowerCase()); //This method - Converts all of the characters in this String to lower case using the rules of the given Locale. System.out.println("toLowerCase(Locale locale) : "+s3.toLowerCase(Locale.ITALY)); //This method - Converts all of the characters in this String to upper case System.out.println("toUpperCase() : "+s1.toUpperCase()); //This method - Converts all of the characters in this String to upper case using the rules of the given Locale. System.out.println("toUpperCase(Locale locale) : "+s3.toUpperCase(Locale.ITALY)); //This method - This object (which is already a string) is itself returned. System.out.println("toString() : "+s1.toString()); //This method - Returns a copy of the string, with leading and trailing whitespace omitted. System.out.println("trim() : "+s1.trim()); //This method - Returns the string representation of the boolean argument. System.out.println("valueOf(boolean b) : "+String.valueOf(false)); //This method - Returns the string representation of the char argument. System.out.println("valueOf(char c) : "+String.valueOf('A')); //This method - Returns the string representation of the char array argument. System.out.println("valueOf(char[] data) : "+String.valueOf(s1.toCharArray())); //This method - Returns the string representation of a specific subarray of the char array argument. System.out.println("valueOf(char[] data, int offset, int count) : "+String.valueOf(s1.toCharArray(),5,8)); //This method - Returns the string representation of the double argument. System.out.println("valueOf(double d) : "+String.valueOf(5.55d)); //This method - Returns the string representation of the float argument. System.out.println("valueOf(float f) : "+String.valueOf(5.55f)); //This method - Returns the string representation of the int argument. System.out.println("valueOf(int i) : "+String.valueOf(123456)); //This method - Returns the string representation of the long argument. System.out.println("valueOf(long i) : "+String.valueOf(9223372036854775807L)); //This method - Returns the string representation of the Object argument. Object ob = new String(s1); System.out.println("valueOf(Object obj) : "+String.valueOf(ob)); } } |
Output:
charAt() : I
codePointAt() : 73
codePointBefore() : 45
codePointCount() : 2
compareTo() : 0
compareToIgnoreCase() : 0
concat() : Java-Identifiers.com
contains() : true
contentEquals(CharSequence c) : true
contentEquals(StringBuffer sb) : true
copyValueOf() : Java-Identifiers
copyValueOf(char[] data, int offset, int count) : Java
endsWith() : false
equals() : true
equalsIgnoreCase() : true
getBytes() : [B@15db9742
getBytes(Charset c) : [B@6d06d69c
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) : Java
hashCode() : -883003969
indexOf(int ch) : 5
indexOf(int ch, int fromIndex) : 5
indexOf(String str) : 5
indexOf(String str,int fromIndex) : 5
intern() : Java-Identifiers
isEmpty() : false
lastIndexOf(int ch) : 13
lastIndexOf(int ch, int fromIndex) : 13
lastIndexOf(String str) : 5
lastIndexOf(String str, int fromIndex) : -1
length() : 16
matches() : false
replace(char oldChar, char newChar) : Pushkar-Identifiers
replace(CharSequence target, CharSequence replacement) : Pushkar
replaceAll(String regex, String replacement) : JavaIdentifiers
replaceFirst(String regex, String replacement) : Jva-Identifiers
split(String regex) : Java
split(String regex, int limit) : Java
startsWith(String prefix) : true
startsWith(String prefix, int toffset) : true
subSequence(int beginIndex, int endIndex) : Ide
substring(int beginIndex) : -Identifiers
substring(int beginIndex, int endIndex) : -Ident
toCharArray() : [C@7852e922
toLowerCase() : java-identifiers
toLowerCase(Locale locale) : java-identificatori
toUpperCase() : JAVA-IDENTIFIERS
toUpperCase(Locale locale) : JAVA-IDENTIFICATORI
toString() : Java-Identifiers
trim() : Java-Identifiers
valueOf(boolean b) : false
valueOf(char c) : A
valueOf(char[] data) : Java-Identifiers
valueOf(char[] data, int offset, int count) : Identifi
valueOf(double d) : 5.55
valueOf(float f) : 5.55
valueOf(int i) : 123456
valueOf(long i) : 9223372036854775807
valueOf(Object obj) : Java-Identifiers
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment