Java Basics:String methods-3

 

Java String charAt()

The Java String charAt() method returns the character at the specified index.

The syntax of the string charAt() method is:

string.charAt(int index)

Here, string is an object of the String class.


charAt() Parameters

  • index - the index of the character (an int value)

charAt() Return Value

  • returns the character at the specified index

Note: If the index passed to chartAt() is negative or out of bounds, it throws an exception.


Example: Java String charAt()

class Main {
  public static void main(String[] args) {
    String str1 = "Learn Java";
    String str2 = "Learn\nJava";

    // first character
    System.out.println(str1.charAt(0)); // 'L'

    // seventh character
    System.out.println(str1.charAt(6)); // 'J'

    // sixth character
    System.out.println(str2.charAt(5)); // '\n'
  }
}

In Java, the index of Strings starts from 0, not 1. That's why chartAt(0) returns the first character. Similarly, charAt(5) and charAt(6) return the sixth and seventh character respectively.


Java String toLowerCase()

The Java String toLowerCase() method converts all characters in the string to lower case characters.

The syntax of the string toLowerCase() method is:

string.toLowerCase()

Here, string is an object of the String class.


toLowerCase() Parameters

The toLowerCase() method does not take any parameters.


toLowerCase() Return Value

  • returns a string with all upper case letters converted to lower case letters

Example: Java toLowerCase()

class Main {
  public static void main(String[] args) {
    String str1 = "Learn Java";
    String str2 = "Java123";

    // convert to lower case letters
    System.out.println(str1.toLowerCase()); // "learn java"
    System.out.println(str2.toLowerCase()); // "java123"
  }
}

As you can see from the above example, toLowerCase() converts all upper case letters to lower case letters.


toLowerCase() With Locale Parameter

The toLowerCase() method can also take a locale as an argument. This allows you to convert characters in a string to lower case using the given Locale (such as: Turkish, Lithuanian etc.) rules.

Its syntax is:

string.toLowerCase(Locale locale)

If you do not pass the locale parameter, the default locale, Locale.getDefault(), is used.

Java String concat()

TheThe Java String concat() method concatenates (joins) two strings and returns it.

The syntax of the string concat() method is:

string.concat(String str)

Here, string is an object of the String class.


concat() Parameters

The concat() method takes a single parameter.

  • str - string to be joined

concat() Return Value

  • returns a string which is the concatenation of string and str (argument string)

Example: Java concat()

class Main {
  public static void main(String[] args) {
    String str1 = "Learn ";
    String str2 = "Java";

    // concatenate str1 and str2
    System.out.println(str1.concat(str2)); // "Learn Java"

    // concatenate str2 and str11
    System.out.println(str2.concat(str1)); // "JavaLearn "
  }
}

Using + Operator for Concatenation

In Java, you can also use the + operator to concatenate two strings. For example,

class Main {
  public static void main(String[] args) {
    String str1 = "Learn ";
    String str2 = "Java";

    // concatenate str1 and str2
    System.out.println(str1 + str2); // "Learn Java"

    // concatenate str2 and str11
    System.out.println(str2 + str1); // "JavaLearn "
  }
}

concat() Vs the + Operator for Concatenation

concat()the + Operator
Suppose, str1 is null and str2 is "Java". Then, str1.concat(str2) throws NullPointerException.Suppose, str1 is null and str2 is "Java". Then, str1 + str2 gives "nullJava".
You can only pass a String to the concat() method.If one of the operands is a string and another is a non-string value. The non-string value is internally converted to a string before concatenation. For example, "Java" + 5 gives "Java5".

 

Java String valueOf()

The Java String valueOf() method returns the string representation of the argument passed.

The syntax of the String valueOf() method for different data types is:

String.valueOf(boolean b)
String.valueOf(char c)
String.valueOf(char[] data)
String.valueOf(double d)
String.valueOf(float f)
String.valueOf(int b)
String.valueOf(long l)
String.valueOf(Object o)

Here, valueOf() is a static method. We call the valueof() method using the class name like this: String.valueOf(b);


valueOf() Parameters

The valueOf() method takes a single parameter.

  • data that is to be converted to a string

valueOf() Return Value

  • returns the string representation of the argument passed

Example: Java String valueOf() for Numbers

class Main {
  public static void main(String[] args) {
    int a = 5;
    long l = -2343834L;
    float f = 23.4f;
    double d = 923.234d;

    // convert numbers to strings
    System.out.println(String.valueOf(a));  // "5"
    System.out.println(String.valueOf(l));  // "-2343834"
    System.out.println(String.valueOf(f));  // "23.4"
    System.out.println(String.valueOf(d));  // "923.234"
  }
}

Example 2: Convert char and char array to String

In Java, you can also use the + operator to concatenate two strings. For example,

class Main {
  public static void main(String[] args) {
    char c = 'J';
    char ch[] = {'J', 'a', 'v', 'a'};

    // convert char to string
    System.out.println(String.valueOf(c));  // "J"

    // convert char array to string
    System.out.println(String.valueOf(ch));  // "Java"
  }
}

Convert subarray of the char Array to String

You can also convert a subarray of a character array to string. For this, we use this syntax.

valueOf(char[] data, int offset, int length)

Here,

  • data - the character array
  • offset - initial offset of the subarray
  • count - the length of the subarray

Example 3: Subarray of a char Array to String

class Main {
  public static void main(String[] args) {
    char ch[] = {'p', 'r', 'o', 'g', 'r', 'a', 'm'};
    int offset = 2;
    int length = 4;
    String result;

    // subarray {'o', 'g', 'r', 'm'} is converted to string
    result = String.valueOf(ch, offset, length);
    System.out.println(result);  // "ogrm"
  }
}

Example 4: Convert Object to String

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {

    ArrayList<String> languages = new ArrayList<String>();
    languages.add("Java");
    languages.add("Python");
    languages.add("Kotlin");

    String result;

    // Output: "[Java, Python, Kotlin]"
    result = String.valueOf(languages);
    System.out.println(result);
  }
}

Here, an ArrayList object, languages, is converted to a string.


In Java, there is another method named copyValueOf() which is equivalent to the valueOf() method.



Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Java OOPS:Constructors in Java – A complete study!!

Java OOPS: OOPS in Selenium