<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>stringMethods &amp;mdash;   christova  </title>
    <link>https://christova.writeas.com/tag:stringMethods</link>
    <description>&lt;b&gt;&lt;h3&gt;Tech Articles&lt;/h3&gt;&lt;/b&gt;&lt;br/&gt;&lt;b&gt;Collated from various sources. Full copyright remains with original authors.&lt;/b&gt;</description>
    <pubDate>Sat, 18 Apr 2026 10:44:32 +0000</pubDate>
    <item>
      <title>Java String Methods</title>
      <link>https://christova.writeas.com/java-string-methods-s6dm?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[&#xA;&#xA;#java #string #methods #stringMethods]]&gt;</description>
      <content:encoded><![CDATA[<p><img src="https://i.snap.as/KRY3Nzog.jpg" alt=""/></p>

<p><a href="https://christova.writeas.com/tag:java" class="hashtag"><span>#</span><span class="p-category">java</span></a> <a href="https://christova.writeas.com/tag:string" class="hashtag"><span>#</span><span class="p-category">string</span></a> <a href="https://christova.writeas.com/tag:methods" class="hashtag"><span>#</span><span class="p-category">methods</span></a> <a href="https://christova.writeas.com/tag:stringMethods" class="hashtag"><span>#</span><span class="p-category">stringMethods</span></a></p>
]]></content:encoded>
      <guid>https://christova.writeas.com/java-string-methods-s6dm</guid>
      <pubDate>Mon, 16 Mar 2026 11:39:43 +0000</pubDate>
    </item>
    <item>
      <title>String Methods in Java</title>
      <link>https://christova.writeas.com/string-methods-in-java?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[&#xA;&#xA;#Java #Strings #StringMethods]]&gt;</description>
      <content:encoded><![CDATA[<p><img src="https://i.snap.as/36uvQEsA.jpg" alt=""/>
<img src="https://i.snap.as/smE04Ul1.jpg" alt=""/></p>

<p><a href="https://christova.writeas.com/tag:Java" class="hashtag"><span>#</span><span class="p-category">Java</span></a> <a href="https://christova.writeas.com/tag:Strings" class="hashtag"><span>#</span><span class="p-category">Strings</span></a> <a href="https://christova.writeas.com/tag:StringMethods" class="hashtag"><span>#</span><span class="p-category">StringMethods</span></a></p>
]]></content:encoded>
      <guid>https://christova.writeas.com/string-methods-in-java</guid>
      <pubDate>Sat, 21 Feb 2026 19:42:01 +0000</pubDate>
    </item>
    <item>
      <title>Java String Methods</title>
      <link>https://christova.writeas.com/java-string-methods?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[In Java, a String is an object that represents a sequence of characters. Strings are instances of the String class, and double quotes (“ “) are used to define string values.&#xA;&#xA;One important thing to remember is that Strings in Java are immutable, which means their values cannot be changed once they are created. Any modification to a string actually creates a new object.&#xA;&#xA;Java provides many built-in String methods that make it easy to perform common operations such as concatenation, comparison, searching, and manipulation. These methods help developers work efficiently with text data in real-world applications.&#xA;&#xA;In this article, we will explore the most important and commonly used Java String methods, along with clear examples to help you understand how they work.&#xA;&#xA;How Do We Create Strings in Java?&#xA;&#xA;A String object can be created in two ways:&#xA;&#xA;String Literal: Using double quotes to create a string&#xA;&#xA;String str = &#34;Hello Java&#34;;&#xA;&#xA;2. Using the new Keyword: Creating a string using the String class&#xA;&#xA;String str = new String(&#34;Hello Java&#34;);&#xA;&#xA;public class StringClass{&#xA;    public static void main(String[] args) {&#xA;        String str = &#34;Hello Java&#34;;&#xA;&#xA;        System.out.println(&#34;Length: &#34; + str.length());  &#xA;        System.out.println(&#34;Uppercase: &#34; + str.toUpperCase());&#xA;        System.out.println(&#34;Substring: &#34; + str.substring(2, 6));&#xA;    }&#xA;}&#xA;&#xA;Commonly Used Java String Methods&#xA;&#xA;Java provides a rich set of String methods that help perform operations such as comparison, searching, and modification of strings. Let’s understand some of the most commonly used methods one by one.&#xA;&#xA;1. indexOf() — Find Character or Substring&#xA;&#xA;The indexOf() method is used to find the position of a character or substring in a string. It returns the index of the first occurrence of the specified character or substring. If the character/substring is not found, it returns -1.&#xA;&#xA;Syntax of indexOf()&#xA;&#xA;public int indexOf​(String str, int fromIndex)&#xA;&#xA;Example&#xA;&#xA;public class FirstOccurrence {&#xA;    public static void main(String[] args) {&#xA;        String str = &#34;Hello, World!&#34;;&#xA;        // Find first occurrence of &#39;l&#39; from index 4&#xA;        int index = str.indexOf(&#39;l&#39;, 4);&#xA;        System.out.println(&#34;Index: &#34; + index); // Output: 9&#xA;    }&#xA;}&#xA;&#xA;2. toCharArray() — Convert String to Character Array&#xA;&#xA;The toCharArray() method converts a string into a character array.&#xA;&#xA;Syntax of toCharArray()&#xA;&#xA;public char[] toCharArray()&#xA;&#xA;Example&#xA;&#xA;public class StringToCharArrayExample {&#xA;    public static void main(String[] args) {&#xA;        String str = &#34;Hello&#34;;&#xA;        // Convert string to char array&#xA;        char[] chars = str.toCharArray();&#xA;&#xA;        // Print each character&#xA;        for (char c : chars) {&#xA;            System.out.print(c + &#34; &#34;);&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;3. equals(Object otherObj) — Compare Two Strings&#xA;&#xA;The equals() method compares two strings for equality. It checks if the content of the strings is the same.&#xA;&#xA;Syntax&#xA;&#xA;public boolean equals(Object otherObj)&#xA;&#xA;Example&#xA;&#xA;public class StringEqualsExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, World!&#34;;&#xA;        // Compare strings using equals()&#xA;        boolean result = s.equals(&#34;Hello, World!&#34;);&#xA;        System.out.println(result); // Output: true&#xA;    }&#xA;}&#xA;&#xA;4. charAt(int index) — Get Character at a Specific Position&#xA;&#xA;The charAt() method is used to find the character at a specific index in a string.&#xA;&#xA;Syntax&#xA;&#xA;public char charAt(int index)&#xA;&#xA;Example&#xA;&#xA;public class CharAtExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, World!&#34;;&#xA;&#xA;        // Get character at index 7&#xA;        char ch = s.charAt(7);&#xA;        System.out.println(ch); // Output: W&#xA;    }&#xA;}&#xA;&#xA;5. concat(String str) — Combine Two Strings&#xA;&#xA;The concat() method is used to append one string to the end of another string.&#xA;&#xA;Syntax&#xA;&#xA;public String concat(String str)&#xA;&#xA;Example&#xA;&#xA;public class ConcatExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Sunil &#34;;&#xA;&#xA;        // Append another string&#xA;        String result = s.concat(&#34;Mishra&#34;);&#xA;        System.out.println(result); // Output: Sunil Mishra&#xA;    }&#xA;}&#xA;&#xA;6. replace() — Replace Characters or Substrings&#xA;&#xA;The replace() method is used to replace characters or substrings in a string.&#xA;&#xA;Syntax&#xA;&#xA;// Replace characters&#xA;public String replace(char oldChar, char newChar)&#xA;&#xA;// Replace substrings&#xA;public String replace(CharSequence target, CharSequence replacement)&#xA;&#xA;Example&#xA;&#xA;public class ReplaceExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, World!&#34;;&#xA;&#xA;        // Replace &#39;l&#39; with &#39;x&#39;&#xA;        String result = s.replace(&#39;l&#39;, &#39;x&#39;);&#xA;        System.out.println(result); // Output: Hexxo, Worxd!&#xA;    }&#xA;}&#xA;&#xA;7. substring() — Extract Part of a String&#xA;&#xA;The substring() method is used to extract a portion of a string from the original string.&#xA;&#xA;Syntax&#xA;&#xA;public String substring(int beginIndex, int endIndex)&#xA;public String substring(int beginIndex) // Extracts till end of string&#xA;&#xA;Example&#xA;&#xA;public class SubstringExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, World!&#34;;&#xA;&#xA;        // Extract substring from index 7 to 12 (exclusive)&#xA;        String part = s.substring(7, 12);&#xA;        System.out.println(part); // Output: World&#xA;    }&#xA;}&#xA;&#xA;8. split() — Split a String into Substrings&#xA;&#xA;The split() method breaks a string into parts based on a given regular expression. It returns an array of strings, not a char array.&#xA;&#xA;Syntax&#xA;&#xA;public String[] split(String regex)          // Split using regex&#xA;public String[] split(String regex, int limit) // Split with limit on number of results&#xA;&#xA;Example&#xA;&#xA;public class SplitExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;apple,banana,orange&#34;;&#xA;&#xA;        // Split string by comma&#xA;        String[] fruits = s.split(&#34;,&#34;);&#xA;&#xA;        // Print each element&#xA;        for (String fruit : fruits) {&#xA;            System.out.println(fruit);&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;9. compareTo() — Compare Two Strings Lexicographically&#xA;&#xA;The compareTo() method compares two strings lexicographically (like in a dictionary) and returns an integer:&#xA;&#xA;Positive number → the current string comes after the argument string&#xA;Negative number → the current string comes before the argument string&#xA;0 → both strings are equal&#xA;&#xA;Syntax&#xA;&#xA;public int compareTo(String anotherString)&#xA;&#xA;Example&#xA;&#xA;public class CompareToExample {&#xA;    public static void main(String[] args) {&#xA;        String s1 = &#34;Hello, World!&#34;;&#xA;        String s2 = &#34;Hello, Java!&#34;;&#xA;&#xA;        // Compare s1 with s2&#xA;        int result = s1.compareTo(s2);&#xA;        System.out.println(result); // Output: positive number&#xA;    }&#xA;}&#xA;&#xA;10. strip() — Remove Leading and Trailing Whitespaces&#xA;&#xA;The strip() method removes all leading and trailing whitespace characters from a string.&#xA;&#xA;Syntax&#xA;&#xA;public String strip()&#xA;&#xA;Example&#xA;&#xA;public class StripExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;   Hello, World!   &#34;;&#xA;&#xA;        // Remove leading and trailing spaces&#xA;        String result = s.strip();&#xA;        System.out.println(&#34;Original: &#39;&#34; + s + &#34;&#39;&#34;);&#xA;        System.out.println(&#34;After strip(): &#39;&#34; + result + &#34;&#39;&#34;);&#xA;    }&#xA;}&#xA;&#xA;11. valueOf() — Convert Data to a String&#xA;&#xA;The valueOf() method returns the string representation of the passed argument. It can convert different types like char, int, boolean, double, char\[\], and even objects into strings.&#xA;&#xA;Syntax&#xA;&#xA;public static String valueOf(char[] data)&#xA;public static String valueOf(int i)&#xA;public static String valueOf(boolean b)&#xA;public static String valueOf(Object obj)&#xA;&#xA;Example&#xA;&#xA;public class ValueOfExample {&#xA;    public static void main(String[] args) {&#xA;        char[] chars = {&#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;};&#xA;&#xA;        // Convert char array to string&#xA;        String s = String.valueOf(chars);&#xA;        System.out.println(s); // Output: Hello&#xA;    }&#xA;}&#xA;&#xA;12. length() — Get Number of Characters in a String&#xA;&#xA;The length() method returns the total number of characters in a string, including letters, numbers, spaces, and special characters.&#xA;&#xA;Syntax&#xA;&#xA;public int length()&#xA;&#xA;Example&#xA;&#xA;public class LengthExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, Sunil!&#34;;&#xA;&#xA;        // Get the length of the string&#xA;        int len = s.length();&#xA;        System.out.println(&#34;Length: &#34; + len);&#xA;    }&#xA;}&#xA;&#xA;13. contains(CharSequence sequence) — Check if String Contains Another String&#xA;&#xA;The contains() method checks whether a string contains a specified sequence of characters (another string). It returns true if the sequence is found, and false otherwise.&#xA;&#xA;Syntax&#xA;&#xA;public boolean contains(CharSequence sequence)&#xA;&#xA;Example&#xA;&#xA;public class ContainsExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, World!&#34;;&#xA;&#xA;        // Check if string contains &#34;World&#34;&#xA;        boolean result = s.contains(&#34;World&#34;);&#xA;        System.out.println(result); // Output: true&#xA;    }&#xA;}&#xA;&#xA;14. startsWith(String prefix) — Check if String Starts with a Prefix&#xA;&#xA;The startsWith() method checks whether a string begins with the specified prefix. It returns true if the string starts with that prefix, and false otherwise.&#xA;&#xA;Syntax&#xA;&#xA;public boolean startsWith(String prefix)&#xA;public boolean startsWith(String prefix, int toffset)&#xA;&#xA;Example&#xA;&#xA;public class StartsWithExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, World!&#34;;&#xA;&#xA;        // Check if string starts with &#34;Hello&#34;&#xA;        boolean result = s.startsWith(&#34;Hello&#34;);&#xA;        System.out.println(result); // Output: true&#xA;&#xA;        // Check from index 7&#xA;        boolean result2 = s.startsWith(&#34;World&#34;, 7);&#xA;        System.out.println(result2); // Output: true&#xA;    }&#xA;}&#xA;&#xA;15. toLowerCase() — Convert All Characters to Lowercase&#xA;&#xA;The toLowerCase() method converts all characters in a string to lowercase.&#xA;&#xA;Syntax&#xA;&#xA;public String toLowerCase()&#xA;&#xA;Example&#xA;&#xA;public class ToLowerCaseExample {&#xA;    public static void main(String[] args) {&#xA;        String s = &#34;Hello, World!&#34;;&#xA;&#xA;        // Convert string to lowercase&#xA;        String lower = s.toLowerCase();&#xA;        System.out.println(lower); // Output: hello, world!&#xA;    }&#xA;}&#xA;&#xA;#JavaStrings #StringMethods #Strings]]&gt;</description>
      <content:encoded><![CDATA[<p>In Java, a String is an object that represents a sequence of characters. Strings are instances of the String class, and double quotes (“ “) are used to define string values.</p>

<p>One important thing to remember is that Strings in Java are immutable, which means their values cannot be changed once they are created. Any modification to a string actually creates a new object.</p>

<p>Java provides many built-in String methods that make it easy to perform common operations such as concatenation, comparison, searching, and manipulation. These methods help developers work efficiently with text data in real-world applications.</p>

<p>In this article, we will explore the most important and commonly used Java String methods, along with clear examples to help you understand how they work.</p>

<h3 id="how-do-we-create-strings-in-java" id="how-do-we-create-strings-in-java"><strong>How Do We Create Strings in Java?</strong></h3>

<p>A String object can be created in two ways:</p>
<ol><li><strong>String Literal:</strong> Using double quotes to create a string</li></ol>

<pre><code>String str = &#34;Hello Java&#34;;
</code></pre>

<p><strong>2. Using the new Keyword:</strong> Creating a string using the String class</p>

<pre><code>String str = new String(&#34;Hello Java&#34;);
</code></pre>

<pre><code>public class StringClass{
    public static void main(String[] args) {
        String str = &#34;Hello Java&#34;;

        System.out.println(&#34;Length: &#34; + str.length());  
        System.out.println(&#34;Uppercase: &#34; + str.toUpperCase());
        System.out.println(&#34;Substring: &#34; + str.substring(2, 6));
    }
}
</code></pre>

<h3 id="commonly-used-java-string-methods" id="commonly-used-java-string-methods"><strong>Commonly Used Java String Methods</strong></h3>

<p>Java provides a rich set of <strong>String methods</strong> that help perform operations such as comparison, searching, and modification of strings. Let’s understand some of the most commonly used methods <strong>one by one</strong>.</p>

<h3 id="1-indexof-find-character-or-substring" id="1-indexof-find-character-or-substring"><strong>1. indexOf() — Find Character or Substring</strong></h3>

<p>The indexOf() method is used to find the position of a character or substring in a string. It returns the index of the first occurrence of the specified character or substring. If the character/substring is not found, it returns -1.</p>

<p><strong>Syntax of indexOf()</strong></p>

<pre><code>public int indexOf​(String str, int fromIndex)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class FirstOccurrence {
    public static void main(String[] args) {
        String str = &#34;Hello, World!&#34;;
        // Find first occurrence of &#39;l&#39; from index 4
        int index = str.indexOf(&#39;l&#39;, 4);
        System.out.println(&#34;Index: &#34; + index); // Output: 9
    }
}
</code></pre>

<h3 id="2-tochararray-convert-string-to-character-array" id="2-tochararray-convert-string-to-character-array"><strong>2. toCharArray() — Convert String to Character Array</strong></h3>

<p>The toCharArray() method converts a string into a character array.</p>

<p><strong>Syntax of toCharArray()</strong></p>

<pre><code>public char[] toCharArray()
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class StringToCharArrayExample {
    public static void main(String[] args) {
        String str = &#34;Hello&#34;;
        // Convert string to char array
        char[] chars = str.toCharArray();

        // Print each character
        for (char c : chars) {
            System.out.print(c + &#34; &#34;);
        }
    }
}
</code></pre>

<h3 id="3-equals-object-otherobj-compare-two-strings" id="3-equals-object-otherobj-compare-two-strings"><strong>3. equals(Object otherObj) — Compare Two Strings</strong></h3>

<p>The equals() method compares two strings for equality. It checks if the content of the strings is the same.</p>

<p><strong>Syntax</strong></p>

<pre><code>public boolean equals(Object otherObj)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class StringEqualsExample {
    public static void main(String[] args) {
        String s = &#34;Hello, World!&#34;;
        // Compare strings using equals()
        boolean result = s.equals(&#34;Hello, World!&#34;);
        System.out.println(result); // Output: true
    }
}
</code></pre>

<h3 id="4-charat-int-index-get-character-at-a-specific-position" id="4-charat-int-index-get-character-at-a-specific-position"><strong>4. charAt(int index) — Get Character at a Specific Position</strong></h3>

<p>The charAt() method is used to find the character at a specific index in a string.</p>

<p><strong>Syntax</strong></p>

<pre><code>public char charAt(int index)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class CharAtExample {
    public static void main(String[] args) {
        String s = &#34;Hello, World!&#34;;

        // Get character at index 7
        char ch = s.charAt(7);
        System.out.println(ch); // Output: W
    }
}
</code></pre>

<h3 id="5-concat-string-str-combine-two-strings" id="5-concat-string-str-combine-two-strings"><strong>5. concat(String str) — Combine Two Strings</strong></h3>

<p>The concat() method is used to append one string to the end of another string.</p>

<p><strong>Syntax</strong></p>

<pre><code>public String concat(String str)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class ConcatExample {
    public static void main(String[] args) {
        String s = &#34;Sunil &#34;;

        // Append another string
        String result = s.concat(&#34;Mishra&#34;);
        System.out.println(result); // Output: Sunil Mishra
    }
}
</code></pre>

<h3 id="6-replace-replace-characters-or-substrings" id="6-replace-replace-characters-or-substrings"><strong>6. replace() — Replace Characters or Substrings</strong></h3>

<p>The replace() method is used to replace characters or substrings in a string.</p>

<p><strong>Syntax</strong></p>

<pre><code>// Replace characters
public String replace(char oldChar, char newChar)

// Replace substrings
public String replace(CharSequence target, CharSequence replacement)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class ReplaceExample {
    public static void main(String[] args) {
        String s = &#34;Hello, World!&#34;;

        // Replace &#39;l&#39; with &#39;x&#39;
        String result = s.replace(&#39;l&#39;, &#39;x&#39;);
        System.out.println(result); // Output: Hexxo, Worxd!
    }
}
</code></pre>

<h3 id="7-substring-extract-part-of-a-string" id="7-substring-extract-part-of-a-string"><strong>7. substring() — Extract Part of a String</strong></h3>

<p>The substring() method is used to extract a portion of a string from the original string.</p>

<p><strong>Syntax</strong></p>

<pre><code>public String substring(int beginIndex, int endIndex)
public String substring(int beginIndex) // Extracts till end of string
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class SubstringExample {
    public static void main(String[] args) {
        String s = &#34;Hello, World!&#34;;

        // Extract substring from index 7 to 12 (exclusive)
        String part = s.substring(7, 12);
        System.out.println(part); // Output: World
    }
}
</code></pre>

<h3 id="8-split-split-a-string-into-substrings" id="8-split-split-a-string-into-substrings"><strong>8. split() — Split a String into Substrings</strong></h3>

<p>The split() method breaks a string into parts based on a given regular expression. It returns an array of strings, not a char array.</p>

<p><strong>Syntax</strong></p>

<pre><code>public String[] split(String regex)          // Split using regex
public String[] split(String regex, int limit) // Split with limit on number of results
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class SplitExample {
    public static void main(String[] args) {
        String s = &#34;apple,banana,orange&#34;;

        // Split string by comma
        String[] fruits = s.split(&#34;,&#34;);

        // Print each element
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}
</code></pre>

<h3 id="9-compareto-compare-two-strings-lexicographically" id="9-compareto-compare-two-strings-lexicographically"><strong>9. compareTo() — Compare Two Strings Lexicographically</strong></h3>

<p>The compareTo() method compares two strings lexicographically (like in a dictionary) and returns an integer:</p>
<ul><li><strong>Positive number</strong> → the current string comes <strong>after</strong> the argument string</li>
<li><strong>Negative number</strong> → the current string comes <strong>before</strong> the argument string</li>
<li><strong>0</strong> → both strings are <strong>equal</strong></li></ul>

<p><strong>Syntax</strong></p>

<pre><code>public int compareTo(String anotherString)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class CompareToExample {
    public static void main(String[] args) {
        String s1 = &#34;Hello, World!&#34;;
        String s2 = &#34;Hello, Java!&#34;;

        // Compare s1 with s2
        int result = s1.compareTo(s2);
        System.out.println(result); // Output: positive number
    }
}
</code></pre>

<h3 id="10-strip-remove-leading-and-trailing-whitespaces" id="10-strip-remove-leading-and-trailing-whitespaces"><strong>10. strip() — Remove Leading and Trailing Whitespaces</strong></h3>

<p>The strip() method removes all leading and trailing whitespace characters from a string.</p>

<p><strong>Syntax</strong></p>

<pre><code>public String strip()
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class StripExample {
    public static void main(String[] args) {
        String s = &#34;   Hello, World!   &#34;;

        // Remove leading and trailing spaces
        String result = s.strip();
        System.out.println(&#34;Original: &#39;&#34; + s + &#34;&#39;&#34;);
        System.out.println(&#34;After strip(): &#39;&#34; + result + &#34;&#39;&#34;);
    }
}
</code></pre>

<h3 id="11-valueof-convert-data-to-a-string" id="11-valueof-convert-data-to-a-string"><strong>11. valueOf() — Convert Data to a String</strong></h3>

<p>The valueOf() method returns the string representation of the passed argument. It can convert different types like <strong>char</strong>, <strong>int</strong>, <strong>boolean</strong>, <strong>double</strong>, <strong>char[],</strong> and even objects into strings.</p>

<p><strong>Syntax</strong></p>

<pre><code>public static String valueOf(char[] data)
public static String valueOf(int i)
public static String valueOf(boolean b)
public static String valueOf(Object obj)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class ValueOfExample {
    public static void main(String[] args) {
        char[] chars = {&#39;H&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;};

        // Convert char array to string
        String s = String.valueOf(chars);
        System.out.println(s); // Output: Hello
    }
}
</code></pre>

<h3 id="12-length-get-number-of-characters-in-a-string" id="12-length-get-number-of-characters-in-a-string"><strong>12. length() — Get Number of Characters in a String</strong></h3>

<p>The length() method returns the total number of characters in a string, including letters, numbers, spaces, and special characters.</p>

<p><strong>Syntax</strong></p>

<pre><code>public int length()
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class LengthExample {
    public static void main(String[] args) {
        String s = &#34;Hello, Sunil!&#34;;

        // Get the length of the string
        int len = s.length();
        System.out.println(&#34;Length: &#34; + len);
    }
}
</code></pre>

<h3 id="13-contains-charsequence-sequence-check-if-string-contains-another-string" id="13-contains-charsequence-sequence-check-if-string-contains-another-string"><strong>13. contains(CharSequence sequence) — Check if String Contains Another String</strong></h3>

<p>The contains() method checks whether a string contains a specified sequence of characters (another string). It returns true if the sequence is found, and false otherwise.</p>

<p><strong>Syntax</strong></p>

<pre><code>public boolean contains(CharSequence sequence)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class ContainsExample {
    public static void main(String[] args) {
        String s = &#34;Hello, World!&#34;;

        // Check if string contains &#34;World&#34;
        boolean result = s.contains(&#34;World&#34;);
        System.out.println(result); // Output: true
    }
}
</code></pre>

<h3 id="14-startswith-string-prefix-check-if-string-starts-with-a-prefix" id="14-startswith-string-prefix-check-if-string-starts-with-a-prefix"><strong>14. startsWith(String prefix) — Check if String Starts with a Prefix</strong></h3>

<p>The startsWith() method checks whether a string begins with the specified prefix. It returns true if the string starts with that prefix, and false otherwise.</p>

<p><strong>Syntax</strong></p>

<pre><code>public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int toffset)
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class StartsWithExample {
    public static void main(String[] args) {
        String s = &#34;Hello, World!&#34;;

        // Check if string starts with &#34;Hello&#34;
        boolean result = s.startsWith(&#34;Hello&#34;);
        System.out.println(result); // Output: true

        // Check from index 7
        boolean result2 = s.startsWith(&#34;World&#34;, 7);
        System.out.println(result2); // Output: true
    }
}
</code></pre>

<h3 id="15-tolowercase-convert-all-characters-to-lowercase" id="15-tolowercase-convert-all-characters-to-lowercase"><strong>15. toLowerCase() — Convert All Characters to Lowercase</strong></h3>

<p>The toLowerCase() method converts all characters in a string to lowercase.</p>

<p><strong>Syntax</strong></p>

<pre><code>public String toLowerCase()
</code></pre>

<p><strong>Example</strong></p>

<pre><code>public class ToLowerCaseExample {
    public static void main(String[] args) {
        String s = &#34;Hello, World!&#34;;

        // Convert string to lowercase
        String lower = s.toLowerCase();
        System.out.println(lower); // Output: hello, world!
    }
}
</code></pre>

<p><a href="https://christova.writeas.com/tag:JavaStrings" class="hashtag"><span>#</span><span class="p-category">JavaStrings</span></a> <a href="https://christova.writeas.com/tag:StringMethods" class="hashtag"><span>#</span><span class="p-category">StringMethods</span></a> <a href="https://christova.writeas.com/tag:Strings" class="hashtag"><span>#</span><span class="p-category">Strings</span></a></p>
]]></content:encoded>
      <guid>https://christova.writeas.com/java-string-methods</guid>
      <pubDate>Tue, 10 Feb 2026 20:28:32 +0000</pubDate>
    </item>
  </channel>
</rss>