Java - String Methods

ZoneTechPro
By -
0

                                

                                  Working with Strings in Java


String Methods
Java - String Methods


Introduction :

Strings are sequences of characters and are widely used in Java programming. Java provides a String class with various built-in methods to handle string operations efficiently.

String Class :

In Java, strings are objects of the String class. Every string variable is an instance of this class, and Java provides multiple methods to manipulate strings, including:


  • concat(): Concatenates two strings.
  • length(): Returns the length of a string.
  • charAt(): Retrieves a character at a specific index.
  • substring(): Extracts a portion of a string.
  • repeat(): Repeats a string multiple times.

String Concatenation :

String concatenation combines two or more strings into a single string.

Using concat() Method :

The concat() method appends one string to another. However, it only works with string values.


class Main {

    public static void main(String[] args) {

        String str1 = "Hello ";

        String str2 = "World";

        String result = str1.concat(str2);

        System.out.println(result);

    }

}


Output:


Hello World

Concatenating a string with a non-string data type using concat() results in an error.


Using + Operator :

The + operator can concatenate multiple strings and other primitive data types, converting non-string values into strings automatically.

class Main {

    public static void main(String[] args) {

        String str1 = "Hello ";

        String str2 = "World ";

        int num = 3;

        String result = str1 + str2 + num;

        System.out.println(result);

    }

}


Output:


Hello World 3


Length of a String :

The length() method returns the number of characters in a string.

class Main {

    public static void main(String[] args) {

        String name = "Rahul";

        int length = name.length();

        System.out.println(length);

    }

}




Output:


5


String Indexing :

Each character in a string has an index, starting from 0. The charAt() method retrieves the character at a specified index.


class Main {

    public static void main(String[] args) {

        String name = "Ram";

        char firstLetter = name.charAt(0);

        System.out.println(firstLetter);

    }

}


Output :


R

String Slicing :

Extracting a portion of a string is called slicing. The substring() method is used for this purpose.

Extracting a Substring :

The substring(startIndex, endIndex) method retrieves a substring from startIndex to endIndex - 1.



class Main {

    public static void main(String[] args) {

        String message = "Welcome to Java";

        String part = message.substring(0, 5);

        System.out.println(part);

    }

}


Output:

Welco


Slicing Till End :

If endIndex is omitted, slicing continues to the end of the string.


class Main {

    public static void main(String[] args) {

        String message = "Welcome to Java";

        String part = message.substring(11);

        System.out.println(part);

    }

}


Output:

Java


String Repetition :

The repeat() method repeats a string n times.


class Main {

    public static void main(String[] args) {

        String str = "Ram";

        System.out.println(str.repeat(2));

    }

}


Output:

RamRam


Summary

  • concat(): Joins two strings.
  • length(): Returns the number of characters in a string.
  • charAt(): Retrieves a character at a specified index.
  • substring(): Extracts a substring.
  • repeat(): Repeats a string multiple times.
Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!