{"id":111,"date":"2019-01-20T20:56:37","date_gmt":"2019-01-20T20:56:37","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=111"},"modified":"2019-03-02T04:38:23","modified_gmt":"2019-03-02T04:38:23","slug":"23-java-string-methods","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/java\/23-java-string-methods\/","title":{"rendered":"Java &#8211; String Methods"},"content":{"rendered":"<p>In this lesson, we are going to learn about various String methods. If you would need to manipulate strings, then these methods are handy.<\/p>\n<p>&nbsp;<\/p>\n<p>Now, we are going to learn about six categories of methods<\/p>\n<ol>\n<li><a href=\"#t1\">Indexing Methods<\/a><\/li>\n<li><a href=\"#t2\">Comparison Methods<\/a><\/li>\n<li><a href=\"#t3\">Modifier Methods<\/a><\/li>\n<li><a href=\"#t4\">Case Modifiers Methods<\/a><\/li>\n<li><a href=\"#t5\">Substring Methods<\/a><\/li>\n<li><a href=\"#t6\">Byte and Character Methods<\/a><\/li>\n<li><a href=\"#t7\">Matching Methods<\/a><\/li>\n<li><a href=\"#t8\">Misc Methods<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Indexing Methods<\/strong><\/h4>\n<p>You you the indexing methods to find positions inside the string.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>int indexOf(int ch)<\/strong><\/p>\n<p>Returns the index within this string of the first occurrence of the given character.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>int indexOf(int ch, int fromIndex)<\/strong><\/p>\n<p>Returns the index within this string of the first occurrence of the given character, starting the search at the specified index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>int indexOf(String str)<\/strong><\/p>\n<p>Returns the index within this string of the first occurrence of the given substring.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><strong>int indexOf(String str, int fromIndex)<\/strong><\/p>\n<p>Returns the index within this string of the first occurrence of the given\u00a0 substring, starting at the specified index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><strong>int lastIndexOf(int ch)<\/strong><\/p>\n<p>Returns the index within this string of the last occurrence of the given\u00a0 character.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><strong>int lastIndexOf(int ch, int fromIndex)<\/strong><\/p>\n<p>Returns the index within this string of the last occurrence of the given character, searching backward starting at the specified index.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><strong>int lastIndexOf(String str)<\/strong><\/p>\n<p>Returns the index within this string of the rightmost occurrence of the given substring.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8.<\/td>\n<td><strong>int lastIndexOf(String str, int fromIndex)<\/strong><\/p>\n<p>Returns the index within this string of the last occurrence of the given substring, searching backward starting at the specified index.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Comparison Methods<\/strong><\/h4>\n<p>We use the comparison to compare a string to another string or object.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<th>SN<\/th>\n<th>Method and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>int compareTo(Object o)<\/strong><\/p>\n<p>Compares this String to the object given as parameter<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>int compareTo(String anotherString)<\/strong><\/p>\n<p>Compares one string with another string lexicographically.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><strong>int compareToIgnoreCase(String str)<\/strong><\/p>\n<p>Compares one string with another string lexicographically, and ignores case differences.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><strong>boolean equals(Object anObject)<\/strong><\/p>\n<p>Compares a string to the specified object and returns true if they are equal. Otherwise returns false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><strong>boolean equalsIgnoreCase(String anotherString)<\/strong><\/p>\n<p>Compares a String with\u00a0 another String, ignoring case considerations. Returns true if the two strings are equal.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><strong>boolean contentEquals(StringBuffer sb)<\/strong><\/p>\n<p>Returns true if and only if this String represents the same sequence of characters as the given StringBuffer. Returns false if otherwise<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><strong>boolean startsWith(String prefix)<\/strong><\/p>\n<p>Checks if this string starts with the given prefix. Returns true if yes, otherwise false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">9<\/td>\n<td><strong>boolean startsWith(String prefix, int toffset)<\/strong><\/p>\n<p>Checks if a string starts with the given prefix beginning a specified index. It returns true is it does. Else, it returns false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">10<\/td>\n<td><strong>boolean endsWith(String suffix)<\/strong><\/p>\n<p>Checks if this string ends with the given suffix.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Modifier Methods<\/strong><\/h4>\n<p>You use these set of methods to modify the value of a string. However, keep in mind that the changes are not persisted. This is because, as you know, Strings are immutable.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td class=\"ts\">1.<\/td>\n<td><strong>String replace(char c1, char c2)<\/strong><\/p>\n<p>It returns a new string that results from replacing all occurrences of c1 in this string with c2<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>String replaceAll(String regex, String replacement)<\/strong><\/p>\n<p>It replaces each substring of the string that matches the specified regular expression with the given replacement.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>String replaceFirst(String regex, String replacement)<\/strong><\/p>\n<p>It replaces the first substring of this string that matches the specified regular expression with the given replacement.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><strong>String[] split(String regex)<\/strong><\/p>\n<p>Splits the string around matches of the specified regular expression. returns a string array<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5.<\/td>\n<td><strong>String[] split(String regex, int limit)<\/strong><\/p>\n<p>Splits a string around matches of the specified regular expression. Returns a string array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">6<\/td>\n<td><strong>String trim()<\/strong><\/p>\n<p>It returns a copy of the string, with leading and trailing whitespace removed.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Case Modifier Methods<\/strong><\/h4>\n<p>Similar to the Modifier methods, the Case Modifier methods also modifies the value of a string. Unlike the modifiers however, case modifier specifically modifies the case of the string.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>String toLowerCase()<\/strong><\/p>\n<p>Converts all of the characters in a String to lower case applying the rules of the default locale.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>String toLowerCase(Locale locale)<\/strong><\/p>\n<p>Converts all of the characters in this String to lower case applying the rules of the specified Locale.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>String toUpperCase()<\/strong><\/p>\n<p>Converts all of the characters in this String to upper case applying the rules of the default locale.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4.<\/td>\n<td><strong>String toUpperCase(Locale locale)<\/strong><\/p>\n<p>Converts all of the characters in this String to upper case applying the rules of the specified Locale.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Substring Methods<\/strong><\/h4>\n<p>You use\u00a0 these methods to find either find substrings within given string<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>String substring(int beginIndex)<\/strong><\/p>\n<p>It finds a substring from a string starting from the given index to the end of the string. Then returns this string<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>String substring(int beginIndex, int endIndex)<\/strong><\/p>\n<p>It finds a substring from a string starting from the given index, beginIndex to the specified endIndex. Then returns this string<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4><strong id=\"t6\"><br \/>\n6. Bytes and Char Methods<\/strong><\/h4>\n<p>These methods are use to retrieve char and byte related information.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>byte getBytes()<\/strong><\/p>\n<p>It creates a sequence of bytes from a string by\u00a0 using the platform&#8217;s default charset, and stores the result into a new byte array. Then returns the new byte array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>byte[] getBytes(String charsetName)<\/strong><\/p>\n<p>It creates a sequence of bytes from a string using the named charset, storing the result into a new byte array. Then returns it.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)<\/strong><\/p>\n<p>Copies characters from this string into the destination character array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4.<\/td>\n<td><strong>char[] toCharArray()<\/strong><\/p>\n<p>It converts the string to a character array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><strong>char charAt(int index)<\/strong><\/p>\n<p>It returns the character at the given index.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. Matching Methods<\/strong><\/h4>\n<p>These methods are used to match the string against regular expressions or other texts.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>boolean matches(String regex)<\/strong><\/p>\n<p>Tells if\u00a0 a string matches the given regular expression. Returns true if it matches.Otherwise, returns false.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>boolean regionMatches(ignoreCase, toffset, String other, int ooffset, int len)<\/strong><\/p>\n<p>Checks whether two string regions are equal.\u00a0 Watch the video for the demo<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><strong>boolean regionMatches(int toffset, String other, int ooffset, int len)<\/strong><\/p>\n<p>Tests whether two string regions are equal. Watch the video for the demo<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t8\">8. Misc Methods<\/strong><\/h4>\n<p>We also have other methods that does not fall into any of the categories. Hence they are miscellaneous methods. Find list of them below.<\/p>\n<table class=\"table table-bordered\">\n<tbody>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><strong>static String valueOf(primitive data type x)<\/strong><\/p>\n<p>This method returns the string representation of the specified data type argument.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><strong>int hashCode()<\/strong><\/p>\n<p>Returns a hash code for the string as integer<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3.<\/td>\n<td><strong>String intern()<\/strong><\/p>\n<p>Returns a canonical representation for the string a particular string.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><strong>int length()<\/strong><\/p>\n<p>Returns the length of this string. That is number of characters in the string<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><strong>String toString()<\/strong><\/p>\n<p>Converts a string to a string. So basically returns the same string!<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">7<\/td>\n<td><strong>static String copyValueOf(char[] data)<\/strong><\/p>\n<p>It returns a String that represents the character sequence in the given character array.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">8<\/td>\n<td><strong>static String copyValueOf(char[] data, int offset, int count)<\/strong><\/p>\n<p>It returns a String that represents the character sequence in the given character array starting from offset to the speficied count.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><strong>String concat(String str)<\/strong><\/p>\n<p>Concatenates the specified string to the end of a string. Combines the two strings together<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we are going to learn about various String methods. If you would need to manipulate strings, then these methods are handy. &nbsp; &hellip; <\/p>\n","protected":false},"author":395,"featured_media":140,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-111","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":4,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/111\/revisions\/143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/140"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}