string methods - coderz.py https://coderzpy.com Keep Coding Keep Cheering! Fri, 17 Jun 2022 11:26:20 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://coderzpy.com/wp-content/uploads/2022/08/cropped-image1-1-32x32.jpg string methods - coderz.py https://coderzpy.com 32 32 Some examples of string class methods https://coderzpy.com/some-examples-of-string-class-methods/ https://coderzpy.com/some-examples-of-string-class-methods/#respond Tue, 14 Jun 2022 20:24:05 +0000 http://coderzpy.com/?p=1595 The methods listed below are some of the most frequently used String class methods in Java. charAt() method: The charAt() ...

The post Some examples of string class methods first appeared on coderz.py.

]]>
The methods listed below are some of the most frequently used String class methods in Java.

charAt() method:

The charAt() function in a string returns the character at the specified index.

class HelloWorld {
    public static void main(String[] args) {
        String myStr = "Hello";
        char result = myStr.charAt(0);
        System.out.println(result);
    }
}
Output:
H
equalsIgnoreCase() method:

equalsIgnoreCase() checks for equality between two Strings while ignoring their case.

        class HelloWorld {
    public static void main(String[] args) {
        String myStr = "Hello",str="Coder";
        boolean result = myStr.equalsIgnoreCase(str);
        System.out.println(result);
    }
}
Output:
false
indexOf() method:

  The index of the first occurrence of a substring or a character is returned by the indexOf() method.

 class HelloWorld {
    public static void main(String[] args) {
       String myStr = "Hello planet earth, you are a great planet.";
    System.out.println(myStr.indexOf("e", 5));
    }
}
Output:
10
length() method:

The length() function of a String returns the number of characters in it.

 class HelloWorld {
    public static void main(String[] args) {
       String myStr = "Hello planet earth, you are a great planet.";
    System.out.println(myStr.length());
    }
}
Output:
43
replace() method:

The replace() method in a string replaces all occurrences of a character with a new character.

public class Demo {
    public static void main(String[] args) {   
        String str = "coderzpy";
        System.out.println(str.replace('c','C'));
    }
}
Output:
Coderzpy
substring() method:

The substring() method returns a part of the string.

public class Demo {
    public static void main(String[] args) {
        String str = "ABCDEFGHIJKLMNO";
        System.out.println(str.substring(4));
        System.out.println(str.substring(4,7));        
    }
}
Output:
EFGHIJKLMNO
EFG
valueOf() method:

String class uses an overloaded version of valueOf() method for all primitive data types and for type Object.

public class Demo {
    public static void main(String[] args) {
         int num = 115;
         String s1 = String.valueOf(num);    //converting int to String
         s1+= "hello";
         System.out.println(s1);
         System.out.println("type of num : "+s1.getClass().getName());   
    }
}
Output:
115hello
type of num : java.lang.String
trim() method:

This method returns a string that has been stripped of any leading and trailing whitespaces.

public class Demo {
    public static void main(String[] args) {
        String str = "   hello coderzpy  ";
        System.out.println(str.trim());   
    }
}
Output:
hello coderzpy
endsWith() method:

the endsWith() method is used to determine whether the string ends with the specified suffix. If the suffix matches the string, it returns true; otherwise, it returns false.

public class Demo {
    public static void main(String[] args) {
        String a="Hello welcome to Coderzpy.com";  
        System.out.println(a.endsWith("m"));  
        System.out.println(a.endsWith("com"));       
    }
}
    
Output:
true
true

Note: also read about the String class methods in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Some examples of string class methods first appeared on coderz.py.

]]>
https://coderzpy.com/some-examples-of-string-class-methods/feed/ 0
StringBuilder class in Java https://coderzpy.com/stringbuilder-class-in-java/ https://coderzpy.com/stringbuilder-class-in-java/#respond Fri, 17 Jun 2022 11:26:19 +0000 http://coderzpy.com/?p=1602 The Java class StringBuilder represents a mutable string of characters. The StringBuilder class offers a substitute for the String Class ...

The post StringBuilder class in Java first appeared on coderz.py.

]]>
  • The Java class StringBuilder represents a mutable string of characters.
  • The StringBuilder class offers a substitute for the String Class in Java since it constructs a mutable sequence of characters instead of an immutable one as the String Class does.
  • The functions of the StringBuilder and StringBuffer classes are extremely similar because both create changeable sequences of characters as an alternative to the String Class.
  • Except for being non-synchronized, the Java StringBuilder class is identical to the StringBuffer class.
  • Constructors of StringBuilder class:
    • StringBuilder(): It generates a blank String Builder with a 16-character initial capacity.
    • StringBuilder(String str): The provided string is used to generate a String Builder.
    • StringBuilder(int length): It generates a new String Builder that is empty and has the given capacity and length.
    A simple example illustrates the use of a constructor:
    // Java Code to illustrate StringBuilder
    
    import java.util.*;
    import java.util.concurrent.LinkedBlockingQueue;
    
    public class Demo {
    	public static void main(String[] argv) throws Exception
    	{
    		// Create a StringBuilder object
    		// using StringBuilder() constructor
    		StringBuilder str = new StringBuilder();
    
    		str.append("Coderzpy");
    
    		// print string
    		System.out.println("String = " + str.toString());
    
    		// create a StringBuilder object
    		// using StringBuilder(CharSequence) constructor
    		StringBuilder str1
    			= new StringBuilder("AAAABBBCCCC");
    
    		// print string
    		System.out.println("String1 = " + str1.toString());
    
    	}
    }
    
    Output:
    String = Coderzpy
    String1 = AAAABBBCCCC
    Methods of StringBuilder class:
    • public StringBuilder append(String s): This string is utilized to append to the provided string. Like append(char), append(boolean), append(int), append(float), append(double), etc., the append() function has many overloads.
    • public StringBuilder insert(int offset, String s): It is used to insert the specified string with this string at the specified position. There are several other overloaded versions of the insert() method, including insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double), etc.
    • public StringBuilder replace(int startIndex, int endIndex, String str): The string from the startIndex and endIndex values are replaced using this method.
    • public StringBuilder delete(int startIndex, int endIndex): It is used to remove the string from startIndex and endIndex that are supplied.
    • public StringBuilder reverse(): It is used to reverse the string.
    • public int capacity(): The current capacity is returned using it.
    • public void ensureCapacity(int minimumCapacity):To make sure the capacity is at least equal to the required minimum, it is used.
    • public char charAt(int index): The character at the requested place is returned using it.
    • public int length(): The length of the string, or the total number of characters, is returned using this method.
    • public String substring(int beginIndex): The substring starting at the supplied beginIndex is returned using it.
    • public String substring(int beginIndex, int endIndex): It is used to retrieve the substring starting at the beginIndex and endIndex values supplied.
    Simple code to illustrate the use of StringBuilder class methods:
    // Java code to illustrate
    // methods of StringBuilder
    
    import java.util.*;
    import java.util.concurrent.LinkedBlockingQueue;
    
    public class Demo1 {
    	public static void main(String[] argv)
    		throws Exception
    	{
    
    		// create a StringBuilder object
    		// with a String pass as parameter
    		StringBuilder str
    			= new StringBuilder("Coderzpy.com");
    
    		// print string
    		System.out.println("String = "
    						+ str.toString());
    
    		// reverse the string
    		StringBuilder reverseStr = str.reverse();
    
    		// print string
    		System.out.println("Reverse String = "
    						+ reverseStr.toString());
    
    		// Append ', '(44) to the String
    		str.appendCodePoint(44);
    
    		// Print the modified String
    		System.out.println("Modified StringBuilder = "
    						+ str);
    
    		// get capacity
    		int capacity = str.capacity();
    
    		// print the result
    		System.out.println("StringBuilder = " + str);
    		System.out.println("Capacity of StringBuilder = "
    						+ capacity);
    	}
    }
    
    Output:
    String = Coderzpy.com
    Reverse String = moc.ypzredoC
    Modified StringBuilder = moc.ypzredoC,
    StringBuilder = moc.ypzredoC,
    Capacity of StringBuilder = 28
    

    Note: also read about the Java StringBuffer class

    Follow Me

    If you like my post, please follow me to read my latest post on programming and technology.

    https://www.instagram.com/coderz.py/

    https://www.facebook.com/coderz.py

    The post StringBuilder class in Java first appeared on coderz.py.

    ]]>
    https://coderzpy.com/stringbuilder-class-in-java/feed/ 0