{"id":174,"date":"2022-01-06T19:39:28","date_gmt":"2022-01-06T14:09:28","guid":{"rendered":"https:\/\/www.scientecheasy.com\/?p=174"},"modified":"2025-05-29T15:44:07","modified_gmt":"2025-05-29T10:14:07","slug":"java-string-class","status":"publish","type":"post","link":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/","title":{"rendered":"String Class in Java | Methods, Examples"},"content":{"rendered":"<p>In an application or a program, it is common to work with the sequence of text character (e.g., for user interactions or data processing). To support character strings, Java programming language provides a <strong>string class<\/strong>. This string class helps to manage text strings effortlessly in the java program.<\/p>\n<p>String class in Java is used to represent character strings. That is, it is used to store multiple characters. For example, strings such as &#8220;12346&#8221; and &#8220;hello world&#8221; are really instances of this class. In Java world, String instances\/objects are usually just called \u201cstrings\u201d.<\/p>\n<p>As we recall, all strings are stored in heap space when we create string using constructors defined in the java.lang.String class.<\/p>\n<p>When we create a string directly in double quote without using constructor, that string is stored in a special area of heap space known as \u201cstring constant pool\u201d.<\/p>\n<h2>String Class Syntax and Hierarchy<\/h2>\n<hr \/>\n<p>The general syntax to declare a string class in java is as follows:<\/p>\n<pre><code class=\"language-java\">public final class String\r\n    extends Object\r\n        implements Serializable, Comparable&lt;String&gt;, CharSequence\r\n<\/code><\/pre>\n<p>Java String class inherits from <em><a href=\"https:\/\/www.scientecheasy.com\/2022\/06\/object-class-in-java.html\/\">java.lang.Object class<\/a><\/em>. So, in addition to the functionality provided by String class, string can also use all the functionality provided by the Object class and can override them.<\/p>\n<p>This class also implements three interfaces, such as CharSequence, Comparable, and Serializable. String in Java is final class, which means it cannot be extended.<\/p>\n<h2>String Constructor<\/h2>\n<hr \/>\n<p>String class provides several constructors in java but for the purpose of exam, we only need to be aware of the following constructors.<\/p>\n<p><strong>1. <span style=\"color: #ff6600;\">String()<\/span>:<\/strong> This constructor creates an empty String.<\/p>\n<p><strong>2. <span style=\"color: #ff6600;\">String(byte[ ] bytes)<\/span>:<\/strong> This constructor constructs a new string object by decoding (or converting) the specified array of bytes using the platform&#8217;s default charset.<\/p>\n<p><strong>3. <span style=\"color: #ff6600;\">String(byte[ ] bytes, Charset charset)<\/span>:<\/strong> It creates a new string object by decoding the specified array of bytes using the specified charset.<\/p>\n<p><strong>4. <span style=\"color: #ff6600;\">String(byte[ ] bytes, int offset, int length)<\/span>:<\/strong> This form of constructor creates a new string object by decoding the specified subarray of bytes using the platform&#8217;s default charset.<br \/>\n[blocksy-content-block id=&#8221;12371&#8243;]<\/p>\n<hr \/>\n<p><strong>5. <span style=\"color: #ff6600;\">String(byte[ ] bytes, int offset, int length, Charset charset)<\/span>:<\/strong> This form of constructor creates a new string object by decoding the specified subarray of bytes using the specified charset.<\/p>\n<p><strong>6. <span style=\"color: #ff6600;\">String(byte[ ] bytes, int offset, int length, String charsetName)<\/span>:<\/strong> This constructor constructs a new string object by decoding the specified subarray of bytes using the specified charset.<\/p>\n<p><strong>7. <span style=\"color: #ff6600;\">String(byte[ ] bytes, String charsetName)<\/span>:<\/strong> It creates a new string object by decoding the specified array of bytes using the specified charset.<\/p>\n<p><strong>8. <span style=\"color: #ff6600;\">String(char[ ] value)<\/span>:<\/strong> This form of constructor allocates a new String so that it represents the sequence of characters currently contained in the character array argument.<\/p>\n<hr \/>\n<p><strong>9. <span style=\"color: #ff6600;\">String(char[ ] value, int offset, int count)<\/span>:<\/strong> This form of constructor allocates a new String that contains characters from a subarray of the character array argument.<\/p>\n<p><strong>10. <span style=\"color: #ff6600;\">String(int[ ] codePoints, int offset, int count)<\/span>:<\/strong> This constructor allocates a new string object that contains characters from a subarray of the Unicode code point array argument.<\/p>\n<p><strong>11. <span style=\"color: #ff6600;\">String(String value)<\/span>:<\/strong> It initializes a newly created string object with the same sequence of characters as the argument string. In other words, the newly created string object is a copy of the argument string.<\/p>\n<p><strong>12. <span style=\"color: #ff6600;\">String(StringBuffer buffer)<\/span>:<\/strong> It allocates a new string object that contains the sequence of characters currently contained in the string buffer argument.<\/p>\n<p><strong>13. <span style=\"color: #ff6600;\">String(StringBuilder builder)<\/span>:<\/strong> This constructor allocates a new string object that contains the sequence of characters currently contained in the string builder argument.<br \/>\n[blocksy-content-block id=&#8221;12121&#8243;]<\/p>\n<h2>List of Methods of String Class in Java<\/h2>\n<hr \/>\n<p>The java.lang.String class provides various useful methods to perform operations like:<\/p>\n<ul>\n<li>convert the string into a character array<\/li>\n<li>Convert numbers into strings<\/li>\n<li>search strings<\/li>\n<li>create substrings<\/li>\n<li>get a string length, and much more.<\/li>\n<\/ul>\n<p>The most important and commonly used methods of the String class in Java programming are as follows:<\/p>\n<ul>\n<li>isEmpty()<\/li>\n<li>length()<\/li>\n<li>replace()<\/li>\n<li>toUpperCase()<\/li>\n<li>toLowerCase()<\/li>\n<li>charAt()<\/li>\n<li>trim()<\/li>\n<li>substring()<\/li>\n<li>contains()<\/li>\n<li>compareTo()<\/li>\n<li>compareToIgnoreCase()<\/li>\n<li>equals()<\/li>\n<li>equalsIgnoreCase()<\/li>\n<li>startsWith()<\/li>\n<li>endsWith()<\/li>\n<li>indexOf()<\/li>\n<li>lastIndexOf()<\/li>\n<li>String[] split()<\/li>\n<\/ul>\n<h2>String Class Methods Example Programs<\/h2>\n<hr \/>\n<p><strong>1. <span style=\"color: #ff0000;\">isEmpty()<\/span>:<\/strong>\u00a0This method is used to check that the string is empty or not. If the length of the string is 0 i.e. the string is empty it will return true otherwise false.<\/p>\n<p>The general syntax for isEmpty() method with an example is as follows:<\/p>\n<pre><code class=\"language-java\">public boolean isEmpty() \/\/ Return type of this method is boolean.\r\n\r\nFor example:\r\n\u00a0 \u00a0 String s=\" \";\r\n\u00a0 \u00a0 \u00a0s.isEmpty(); \/\/ It will return true because string is empty.<\/code><\/pre>\n<p>Let&#8217;s take a simple example program based on isEmpty() method.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class IsEmptyTest {\r\npublic static void main(String[] args) \r\n{\r\n  String str = \"\"; \/\/ Empty string.\r\n  boolean isEmpty1 = str.isEmpty();\r\n  System.out.println(\"Is String empty: \" +isEmpty1);\r\n\r\n  String str2 = \" \";\r\n  boolean isEmpty2 = str2.isEmpty();\r\n  System.out.println(\"Is String empty: \" +isEmpty2);\r\n }\r\n}<\/code><\/pre>\n<pre>Output:\r\n      Is String empty: true\r\n      Is String empty: false<\/pre>\n<p><strong>2. <span style=\"color: #ff0000;\">length()<\/span>:<\/strong>\u00a0The number of characters in a string is called length of a string. To get this value, call length() method.<\/p>\n<p>The length() method returns length or number of characters of a string. It counts all characters, numbers, and symbols enclosed in &#8221; &#8220;. The return type of this method is an integer value.<\/p>\n<p>The general syntax of length() method with an example is given below:<\/p>\n<pre><code class=\"language-java\">public int length()\r\n\r\nFor example:\r\n\u00a0 String s = \"Hello\";\r\n\u00a0  s.length(); \/\/ 5 returns the number of characters in the string s.<\/code><\/pre>\n<p><strong><span style=\"color: #0000ff;\">Note<\/span>:<\/strong> s.length; Here, length is a variable that is applicable for an array but not for string. So, you do not confuse between s.length and s.length().<br \/>\n[blocksy-content-block id=&#8221;12153&#8243;]<br \/>\n<strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class LengthTest {\r\npublic static void main(String[] args) \r\n{\r\n    String str = \"\"; \/\/ Empty string.\r\n \/\/ Find the length of string.\r\n    int length = str.length();\r\n    System.out.println(\"Length of string: \" +length);\r\n\r\n    String str2 = \"I like Scientech Easy\";\r\n    int length2 = str2.length();\r\n    System.out.println(\"Length of string: \" +length2);\r\n\r\n    char arr[] = {'S','c','i','e','n','t','e','c','h'};\r\n    String str3 = new String(arr);\r\n    System.out.println(\"Length of characters: \" +str3.length());\r\n  }\r\n}<\/code><\/pre>\n<pre>Output:\r\n       Length of string: 0\r\n       Length of string: 21\r\n       Length of characters: 9<\/pre>\n<p><strong>3. <span style=\"color: #ff0000;\">replace(char old, char new)<\/span>:<\/strong> This method is introduced in JDK 1.5. It replaces all the old char or CharSequence to new char or CharSequence. It has the following general syntax:<\/p>\n<pre><code class=\"language-java\">String replace(char old, char new)\r\n\r\nFor example:\r\n\u00a0  String s1=\"Kava\";\r\n\u00a0 \u00a0 s1.replace('K', 'J'); \/\/ The returned string will be \"Java\".<\/code><\/pre>\n<p>Let&#8217;s take an example program based on the replace() method.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class ReplaceExample {\r\npublic static void main(String[] args)\r\n{\r\n  String str = \"Programming\";\r\n  String s1 = str.replace('m', 'n');\r\n  System.out.println(\"str.replace('m', 'n'): \" +s1);\r\n } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n      str.replace('m', 'n'): Progranning\r\n<\/pre>\n<p><strong>4. <span style=\"color: #ff6600;\">toUpperCase()<\/span>:<\/strong>\u00a0The toUpperCase() method converts all the lowercase letters of a string into upper case and returns that upper-cased string.<\/p>\n<pre><code class=\"language-java\">public String toUpperCase()\r\n<\/code><\/pre>\n<p>To understand more detail with example programs and memory concepts, go to this tutorial: <a href=\"https:\/\/www.scientecheasy.com\/2019\/04\/java-string-touppercase.html\/\"><em>Java String toUpperCase() method<\/em>.<\/a><\/p>\n<p><strong>5. <span style=\"color: #ff6600;\">toLowerCase()<\/span>:<\/strong> This method converts all the uppercase letters of a string into lower case and returns that lower-cased string.<\/p>\n<pre><code class=\"language-java\">public String toLowerCase()\r\n<\/code><\/pre>\n<p>To understand in more detail with example programs, go to this tutorial: <em><a href=\"https:\/\/www.scientecheasy.com\/2021\/12\/java-string-tolowercase.html\/\">Java String toLowerCase() method<\/a><\/em>.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class CaseExample {\r\npublic static void main(String[] args)\r\n{\r\n  String str = \"TECHNOLOGY\";\r\n  System.out.println(\"str.toLowerCase(): \" +str.toLowerCase());\r\n  String s = \"programming\";\r\n  System.out.println(\"s.toUpperCase(): \" +s.toUpperCase());\r\n  } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n     str.toLowerCase(): technology\r\n     s.toUpperCase(): PROGRAMMING\r\n<\/pre>\n<p><strong>6. <span style=\"color: #ff6600;\">charAt(int index)<\/span>:<\/strong>\u00a0The charAt() method returns the character located at specified index. It accepts an integer value that represents an index number. The index number starts at 0 and goes to (n-1) where n is the length of the string.<\/p>\n<p>If the specified index number is greater than or equal to this string length or a negative number, it will return StringIndexOutOfBoundsException. The basic syntax of this method is given below:<\/p>\n<pre><code class=\"language-java\">public char charAt(int index)\r\n<\/code><\/pre>\n<p>For example, suppose we call this method as s.charAt(4); then it will give the 4th character in the string s.<\/p>\n<p>To get more example programs based on this method, go to this tutorial: <em><a href=\"https:\/\/www.scientecheasy.com\/2021\/12\/get-character-from-string-in-java.html\/\">How to get character from string in Java<\/a><\/em><\/p>\n<hr \/>\n<p><strong>7. <span style=\"color: #ff6600;\">trim()<\/span>:<\/strong> This trim() method removes spaces from the beginning and ending from a string.<\/p>\n<p>For example, suppose a string is written as &#8221;\u00a0 Scientech Easy\u00a0 &#8220;, the spaces before &#8220;Scientech&#8221; and after &#8220;Easy&#8221; is unnecessary. So, it can be removed by calling trim() method. It has the following general form.<\/p>\n<pre><code class=\"language-java\">public String trim()\r\n<\/code><\/pre>\n<p>This method does not remove spaces from the middle of a string. For example, the space between &#8220;Scientech&#8221; and &#8220;Easy&#8221; cannot be removed using the trim() method.<\/p>\n<hr \/>\n<p>Let&#8217;s create a Java program where we will remove space from beginning and end in a string.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class RemovingSpace {\r\npublic static void main(String[] args)\r\n{\r\n    String str = new String(\" Scientech Easy \");\r\n    System.out.println(\"Original string: \" +str);\r\n \/\/ Removing space from beginning and end.\r\n    String trimElement = str.trim();\r\n    System.out.println(\"After removing space, String: \" +trimElement);\r\n  } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n    Original string:  Scientech Easy \r\n    After removing space, String: Scientech Easy\r\n<\/pre>\n<p><strong>8. <span style=\"color: #ff6600;\">substring(int startIndex)<\/span>:<\/strong> This method returns a new string that is a substring of this string. Here, startIndex represents the index at which substring begins with a character.<\/p>\n<p>The substring starts at startIndex and executes until the end of the string. It has the following general form.<\/p>\n<pre><code class=\"language-java\">public String substring(int startIndex)\r\n\r\nFor example,\r\n     String s = \"India\";\r\n      s.substring(3);\r\n<\/code><\/pre>\n<p>Here, startIndex is 3. So, it will return characters starting 3rd character till the end of s.<\/p>\n<p><strong>9. <span style=\"color: #ff6600;\">substring(int startIndex, int endIndex)<\/span>:<\/strong> This method returns a new string of all the characters from starting index up to ending index but not including, the ending index. For example,<\/p>\n<pre><code class=\"language-java\">String s=\"India\";\r\n  s.substring(1,3);\r\n<\/code><\/pre>\n<p>It will return the characters of s starting from 1st to 2nd positions. To practice more example programs, go to this tutorial: <em><a href=\"https:\/\/www.scientecheasy.com\/2020\/05\/java-substring.html\/\">Substring in Java<\/a><\/em><\/p>\n<hr \/>\n<p><strong>10. <span style=\"color: #ff6600;\">contains(CharSequence s)<\/span>:<\/strong>\u00a0This method returns true if this string contains the specified sequence of char value. It has the following general form:<\/p>\n<pre><code class=\"language-java\">boolean contains(CharSequence s)\r\n<\/code><\/pre>\n<p>Let&#8217;s take an example program base don this method.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class ContainsExample {\r\npublic static void main(String[] args)\r\n{\r\n   String str = new String(\"I love Java\");\r\n   boolean str2 = str.contains(\"Love\");\r\n   boolean str3 = str.contains(\"Java\");\r\n   System.out.println(\"str2: \" +str2);\r\n   System.out.println(\"str3: \" +str3);\r\n  } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n     str2: false\r\n     str3: true\r\n<\/pre>\n<p><strong>11. <span style=\"color: #ff6600;\">compareTo(String str)<\/span>:<\/strong> This method compares two strings and to know which is bigger or smaller. It can be used as: s1.compareTo(s2). Here, s1 and s2 are string compared.<\/p>\n<p>If s1 and s2 strings are equal, this method provides 0. If s1 is greater than s2, it returns positive number. If s1 is less than s2, it returns a negative number. It has the following general form:<\/p>\n<pre><code class=\"language-java\">int compareTo(String str)\r\n<\/code><\/pre>\n<p><strong>12. <span style=\"color: #ff6600;\">compareToIgnoreCase(String str)<\/span>:<\/strong> It is same as compareTo() method. This method does not take the case of strings into consideration. This means &#8220;LOVE&#8221; and &#8220;love&#8221; will be the same for this method.<\/p>\n<p>The general form of this method is as follows:<\/p>\n<pre><code class=\"language-java\">int compareToIgnoreCase(String str)\r\n<\/code><\/pre>\n<p>Let&#8217;s create a simple Java program in which we will compare two strings using these two methods.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class StringCompare {\r\npublic static void main(String[] args)\r\n{\r\n  String str = \"Love\";\r\n  String s = \"love\";\r\n  int x = str.compareTo(s);\r\n  System.out.println(\"str.compareTo(s): \" +x);\r\n  int y = str.compareToIgnoreCase(s);\r\n  System.out.println(\"str.compareToIgnoreCase(s): \" +y);\r\n  } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n    str.compareTo(s): -32\r\n    str.compareToIgnoreCase(s): 0\r\n<\/pre>\n<p><strong>13. <span style=\"color: #ff6600;\">equals(String str)<\/span>:<\/strong> This method returns true if two strings are equal or same. It is case sensitive and can be used as s1.equals(s2);. The general form of this method is as follows:<\/p>\n<pre><code class=\"language-java\">boolean equals(String str)\r\n<\/code><\/pre>\n<p><strong>14. <span style=\"color: #ff6600;\">equalsIgnoreCase(String s)<\/span>:<\/strong> This method is the same as equals() method, but it does case-insensitive comparison. The general syntax is as follows:<\/p>\n<pre><code class=\"language-java\">boolean equalsIgnoreCase(String s)\r\n<\/code><\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class StringCompare {\r\npublic static void main(String[] args)\r\n{\r\n  String str = \"Love\";\r\n  String s = \"love\";\r\n  boolean x = str.equals(s);\r\n  System.out.println(\"str.equals(s): \" +x);\r\n  boolean y = str.equalsIgnoreCase(s);\r\n  System.out.println(\"str.equalsIgnoreCase(s): \" +y);\r\n  } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n      str.equals(s): false\r\n      str.equalsIgnoreCase(s): true\r\n<\/pre>\n<p><strong>15. <span style=\"color: #ff6600;\">startsWith(String s)<\/span>:<\/strong> This method returns true if a string starts with the specified substring s. To invoke this method, use this format: s1.startsWith(s2);. If s1 starts with s2, it returns true, otherwise false. This method is case sensitive. The general form is like this:<\/p>\n<pre><code class=\"language-java\">boolean startsWith(String s)\r\n<\/code><\/pre>\n<p><strong>16. <span style=\"color: #ff6600;\">endsWith(String s)<\/span>:<\/strong> This method returns true if a string ends with the specified substring s, otherwise, it returns false. This method is also case-sensitive. The general form of this method is like this:<\/p>\n<pre><code class=\"language-java\">boolean endsWith(String s)\r\n<\/code><\/pre>\n<p>Let&#8217;s take an example program based on the above both methods.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class StartsEndsExample {\r\npublic static void main(String[] args)\r\n{\r\n  String str = \"Technology\";\r\n  boolean x = str.startsWith(\"c\");\r\n  System.out.println(\"str.startsWith(): \" +x);\r\n  boolean y = str.endsWith(\"y\");\r\n  System.out.println(\"str.endsWith(): \" +y);\r\n  } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n      str.startsWith(): false\r\n      str.endsWith(): true\r\n<\/pre>\n<p><strong>19. <span style=\"color: #ff6600;\">indexOf(String str)<\/span>:<\/strong> This method is used to get index of the first occurrence of specified substring in the main string. It returns integer value as index. The general syntax is like this:<\/p>\n<pre><code class=\"language-java\">public int indexOf(String str)\r\n<\/code><\/pre>\n<p><strong>20. <span style=\"color: #ff6600;\">lastIndexOf(String str)<\/span>:<\/strong> This method is similar to the preceding method, but returns the last occurrence of substring str in the main string. If str is not found, it returns false.<\/p>\n<pre><code class=\"language-java\">public int lastIndexOf(String str)\r\n<\/code><\/pre>\n<p>Let&#8217;s create a program in which we will get the index value of the first and last occurrences of the given string.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class IndexExample {\r\npublic static void main(String[] args)\r\n{\r\n  String str = \"Programming\";\r\n  int x = str.indexOf(\"m\");\r\n  System.out.println(\"str.indexOf(): \" +x);\r\n  System.out.println(\"str.lastIndexOf(): \" +str.lastIndexOf(\"g\"));\r\n  } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n      str.indexOf(): 6\r\n      str.lastIndexOf(): 10\r\n<\/pre>\n<p><strong>21. <span style=\"color: #ff6600;\">String[ ] split(delimiter)<\/span>:<\/strong> This method is used to break a string into pieces at the place represented by the delimiter. It returns the resultant pieces in the form string type array.<\/p>\n<p>For example, suppose the delimiter is comma, the string is split (or cut) pieces whereever &#8216;,&#8217; if found. The general syntax to define this method is as follows:<\/p>\n<pre><code class=\"language-java\">public String[ ] split(delimiter)\r\n<\/code><\/pre>\n<p>Let&#8217;s create a program for splitting a string into pieces whenever a space is found.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code class=\"language-java\">public class SplitExample {\r\npublic static void main(String[] args)\r\n{\r\n   String str = \"I love Java Technology\";\r\n   String[ ] s;\r\n       s = str.split(\" \");\r\n   int length = str.length();\r\n   for(int i = 0;  i &lt; length; i++) {\r\n\t  System.out.println(s[i]);\r\n   }\r\n } \r\n}\r\n<\/code><\/pre>\n<pre>Output:\r\n       I\r\n       love\r\n      Java\r\n      Technology\r\n<\/pre>\n<p>In this tutorial, you learned String class and its various forms of constructors and methods in Java with the help of examples. I hope that you will have understood the basic definition of string class methods and practiced all example programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an application or a program, it is common to work with the sequence of text character (e.g., for user interactions or data processing). To support character strings, Java programming language provides a string class. This string class helps to manage text strings effortlessly in the java program. String class in Java is used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_gspb_post_css":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","hentry","category-java"],"blocksy_meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>String Class in Java | Methods, Examples - Scientech Easy<\/title>\n<meta name=\"description\" content=\"Learn String class in Java and its hierarchy, constructors provided by string class, various methods of string class with example programs\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"String Class in Java | Methods, Examples - Scientech Easy\" \/>\n<meta property=\"og:description\" content=\"Learn String class in Java and its hierarchy, constructors provided by string class, various methods of string class with example programs\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/\" \/>\n<meta property=\"og:site_name\" content=\"Scientech Easy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/scientecheasy\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/D.gupta008\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-06T14:09:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-29T10:14:07+00:00\" \/>\n<meta name=\"author\" content=\"DEEPAK GUPTA\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DEEPAK GUPTA\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/\"},\"author\":{\"name\":\"DEEPAK GUPTA\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#\\\/schema\\\/person\\\/b5bb668584010e27bf869ba72f0fd7f2\"},\"headline\":\"String Class in Java | Methods, Examples\",\"datePublished\":\"2022-01-06T14:09:28+00:00\",\"dateModified\":\"2025-05-29T10:14:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/\"},\"wordCount\":1772,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#organization\"},\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/\",\"url\":\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/\",\"name\":\"String Class in Java | Methods, Examples - Scientech Easy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#website\"},\"datePublished\":\"2022-01-06T14:09:28+00:00\",\"dateModified\":\"2025-05-29T10:14:07+00:00\",\"description\":\"Learn String class in Java and its hierarchy, constructors provided by string class, various methods of string class with example programs\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/2022\\\/01\\\/java-string-class.html\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scientecheasy.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"String Class in Java | Methods, Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#website\",\"url\":\"https:\\\/\\\/www.scientecheasy.com\\\/\",\"name\":\"Scientech Easy\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scientecheasy.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#organization\",\"name\":\"Scientech Easy\",\"url\":\"https:\\\/\\\/www.scientecheasy.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.scientecheasy.com\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/scientecheasy-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.scientecheasy.com\\\/wp-content\\\/uploads\\\/2024\\\/06\\\/scientecheasy-logo.png\",\"width\":119,\"height\":119,\"caption\":\"Scientech Easy\"},\"image\":{\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/scientecheasy\\\/\",\"https:\\\/\\\/medium.com\\\/scientech-easy\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/#\\\/schema\\\/person\\\/b5bb668584010e27bf869ba72f0fd7f2\",\"name\":\"DEEPAK GUPTA\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.scientecheasy.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a4855f82604819e190e1d130415225c7.jpg?ver=1778108696\",\"url\":\"https:\\\/\\\/www.scientecheasy.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a4855f82604819e190e1d130415225c7.jpg?ver=1778108696\",\"contentUrl\":\"https:\\\/\\\/www.scientecheasy.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a4855f82604819e190e1d130415225c7.jpg?ver=1778108696\",\"caption\":\"DEEPAK GUPTA\"},\"description\":\"Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad. He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/D.gupta008\",\"https:\\\/\\\/www.instagram.com\\\/deepak_scientecheasy\\\/\",\"www.linkedin.com\\\/in\\\/deepak-kumar-gupta-41993414a\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"String Class in Java | Methods, Examples - Scientech Easy","description":"Learn String class in Java and its hierarchy, constructors provided by string class, various methods of string class with example programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/","og_locale":"en_US","og_type":"article","og_title":"String Class in Java | Methods, Examples - Scientech Easy","og_description":"Learn String class in Java and its hierarchy, constructors provided by string class, various methods of string class with example programs","og_url":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/","og_site_name":"Scientech Easy","article_publisher":"https:\/\/www.facebook.com\/scientecheasy\/","article_author":"https:\/\/www.facebook.com\/D.gupta008","article_published_time":"2022-01-06T14:09:28+00:00","article_modified_time":"2025-05-29T10:14:07+00:00","author":"DEEPAK GUPTA","twitter_card":"summary_large_image","twitter_misc":{"Written by":"DEEPAK GUPTA","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/#article","isPartOf":{"@id":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/"},"author":{"name":"DEEPAK GUPTA","@id":"https:\/\/www.scientecheasy.com\/#\/schema\/person\/b5bb668584010e27bf869ba72f0fd7f2"},"headline":"String Class in Java | Methods, Examples","datePublished":"2022-01-06T14:09:28+00:00","dateModified":"2025-05-29T10:14:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/"},"wordCount":1772,"commentCount":0,"publisher":{"@id":"https:\/\/www.scientecheasy.com\/#organization"},"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/","url":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/","name":"String Class in Java | Methods, Examples - Scientech Easy","isPartOf":{"@id":"https:\/\/www.scientecheasy.com\/#website"},"datePublished":"2022-01-06T14:09:28+00:00","dateModified":"2025-05-29T10:14:07+00:00","description":"Learn String class in Java and its hierarchy, constructors provided by string class, various methods of string class with example programs","breadcrumb":{"@id":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scientecheasy.com\/2022\/01\/java-string-class.html\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scientecheasy.com\/"},{"@type":"ListItem","position":2,"name":"String Class in Java | Methods, Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.scientecheasy.com\/#website","url":"https:\/\/www.scientecheasy.com\/","name":"Scientech Easy","description":"","publisher":{"@id":"https:\/\/www.scientecheasy.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scientecheasy.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.scientecheasy.com\/#organization","name":"Scientech Easy","url":"https:\/\/www.scientecheasy.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.scientecheasy.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.scientecheasy.com\/wp-content\/uploads\/2024\/06\/scientecheasy-logo.png","contentUrl":"https:\/\/www.scientecheasy.com\/wp-content\/uploads\/2024\/06\/scientecheasy-logo.png","width":119,"height":119,"caption":"Scientech Easy"},"image":{"@id":"https:\/\/www.scientecheasy.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/scientecheasy\/","https:\/\/medium.com\/scientech-easy"]},{"@type":"Person","@id":"https:\/\/www.scientecheasy.com\/#\/schema\/person\/b5bb668584010e27bf869ba72f0fd7f2","name":"DEEPAK GUPTA","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.scientecheasy.com\/wp-content\/litespeed\/avatar\/a4855f82604819e190e1d130415225c7.jpg?ver=1778108696","url":"https:\/\/www.scientecheasy.com\/wp-content\/litespeed\/avatar\/a4855f82604819e190e1d130415225c7.jpg?ver=1778108696","contentUrl":"https:\/\/www.scientecheasy.com\/wp-content\/litespeed\/avatar\/a4855f82604819e190e1d130415225c7.jpg?ver=1778108696","caption":"DEEPAK GUPTA"},"description":"Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad. He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.","sameAs":["https:\/\/www.facebook.com\/D.gupta008","https:\/\/www.instagram.com\/deepak_scientecheasy\/","www.linkedin.com\/in\/deepak-kumar-gupta-41993414a"]}]}},"_links":{"self":[{"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":0,"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scientecheasy.com\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}