- A string is a sequence of characters.
- String is a class but can be used like a data type.
- A string is initiated as follows.
Syntax:
String name;
name = new String("Learner");
We can use the following ways to print in Java :
System.out.print();
System.out.println();
System.out.printf();
System.out.format();
System.out.printf(“%c”,ch);
- %c for char.
- %d for int.
- %f for float.
- %s for string.
String Method-
String method operate on java strings.They can be used to find length of the string,convert to lowercase,etc.
Some of the commonly used String Methods are:
Methods
name.length()
name.toLowerCase()
name.toUpperCase()
name.trim()
name.substring(int start)
name.substring(int start,int end)
name.replace(‘r’,’p’)
name.startswith(“Lea”)
name.endswith(“rner”)
name.charAt(2)
name.indexOf(s)
name.indexOf(“s”,3)
name.lastIndexOf(“r”)
name.lastIndexOf(“r”,2)
nam.equals(“Learner”)
name.equalsIgnoreCase
(“Learner”)
Uses
Returns length of the String name.
Returns a new string which has all the lowercase characters from the String name.
Returns a new String which has all the uppercase characters from the String name.
Returns a new String after removing all the leading and traveling spaces from the original String.
Returns a substring from the start to the end of substring(4) returns “rner”.
Returns a substring from start index to the end index. Start index is included and excluded.
Returns a new String after replacing r with p.Leapnep(Learner) is returned in this case.
Return true if name starts with String “Lea” true in this case .
Return true if name ends with String “rner” true in this case.
Return character at a given index position ‘a’ in this case.
Return the index of the given String .for ex-name.index of(“ea”) returns 1 which is the first occurence of ea in String “Learner”,-1 otherwise.
Return the index of the given String starting from the index 3(int).-1 is returned in this case.
Return the last index of the given string .3 in this case.
Return the last index of of the given String before index 2.
Return true if the given String is equal to “Learner” false otherwise [case sensitive].
Return true if two String are equal ignoring the case of characters.