String is a collection of stream of characters.Following are some most basic string functions:
WAP to implement simple string methods.
class methods{
public static void main(String args[])
{
String s="maninder ";
String s1="Maninder ";
System.out.println(s.charAt(4)+"\n");
System.out.println(s.indexOf("i")+"\n");
System.out.println(s.lastIndexOf("n")+"\n");
System.out.println(s.endsWith("d")+"\n");
System.out.println(s.startsWith("m")+"\n");
System.out.println(s.equals(s1)+"\n");
System.out.println(s.equalsIgnoreCase(s1)+"\n");
}
}
Output of the program:
1. CharAt(): To extract a single character from a string.This method checks the value of the given position.
2. indexOf(): It checks the position of the given value.
3. lastIndexOf: It checks the last position of the given value.
4. endsWith:This function gives result in Boolean expression. If the given value matches at the end of the stored string then the result is true otherwise false.
5. startWith:This function gives result in Boolean expression. If the given value is matches at the start of the stored string then the result is true otherwise false.
6. equals: This function gives result in Boolean expression.If the two string are equals on the basis of characters then the result is true otherwise false.It also considered the upper and lower case of the characters.
7. equalsIgnoreCase: This function gives result in Boolean expression.If the two string are equals on the basis of characters and it also ignore upper and lower case of the characters then the result is true otherwise false.
3. lastIndexOf: It checks the last position of the given value.
4. endsWith:This function gives result in Boolean expression. If the given value matches at the end of the stored string then the result is true otherwise false.
5. startWith:This function gives result in Boolean expression. If the given value is matches at the start of the stored string then the result is true otherwise false.
6. equals: This function gives result in Boolean expression.If the two string are equals on the basis of characters then the result is true otherwise false.It also considered the upper and lower case of the characters.
7. equalsIgnoreCase: This function gives result in Boolean expression.If the two string are equals on the basis of characters and it also ignore upper and lower case of the characters then the result is true otherwise false.
WAP to implement simple string methods.
class methods{
public static void main(String args[])
{
String s="maninder ";
String s1="Maninder ";
System.out.println(s.charAt(4)+"\n");
System.out.println(s.indexOf("i")+"\n");
System.out.println(s.lastIndexOf("n")+"\n");
System.out.println(s.endsWith("d")+"\n");
System.out.println(s.startsWith("m")+"\n");
System.out.println(s.equals(s1)+"\n");
System.out.println(s.equalsIgnoreCase(s1)+"\n");
}
}
Output of the program:
No comments:
Post a Comment