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)
- 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.
- 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.
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.
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.
The toLowerCase()
method does not take any parameters.
toLowerCase() Return Value
- returns a string with all upper case letters converted to lower case letters
- 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.
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.
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.
Comments
Post a Comment