String Handling
String - It is a collection of characters.
Program-
public class str
{
public static void main(String args[])
{
String data;
data = "Mynameiskhan";
System.out.println(data);
}
}
Functions used in String handling-
-> used for output
1. charAt(index) - it is used to display a character in the string, by giving the appropriate index.
ex- String data1 = "Chitransh";
data1.charAt(2);
-> i
2. codePointAt(index) - it is used to display the ascii value of given character.
ex- String data1 = "Chitransh";
data1.codePointAt(3);
-> 116(ascii code of t)
3. codePointBefore(index) - giving the ascii code of the character before the given index of the character.
ex- String data1= "Chitransh";
data1.codePointBefore(3);
-> 105(ascii code of i) // index is given 3 but displays the output for index position 2.
4. compareTo() - it returns the values in the form of 0(if true) or non zero(if false).
ex-String data1 = "Chitransh";
String data2 = "chitransh";
data1.compareTo(data2);
-> -3
here, the function will return the value other than zero(Case is different).
5. compareToIgnoreCase() - It will compare two string values ignoring the case of their strings.
ex-String data1 = "Chitransh";
String data2 = "chitransh";
data1.compareToIgnoreCase(data2);
-> 0
here, the function will return 0 instead of different case in two strings
6. equals() - it compares two strings and returns the value true or false.
ex- String data1 = "Chitransh";
String data2 = "Chitransh";
data1.equals(data2);
-> true
7. concat() - it is used to concatinate two strings.
ex- String data1 = "Chitransh";
String data2 = "Thakur";
data1.concat(data2);
-> Chitransh Thakur
8. toUpperCase() - it converts the string value into upper case.
ex- String data1 = "chitransh";
data1.toUpperCase();
-> CHITRANSH
9. toLowerCase() - it converts the string value into lower case.
ex- String data1 = "ChiTransh";
data1.toLowerCase();
-> chitransh
10. isEmpty() - checks whether the string is empty or not.
ex- String data1 = "";
if(data1.isEmpty())
{
System.out.println("String is empty");
}
-> String is empty
Comments
Post a Comment