Introduction
String class
It is derived from the class object.
The class object is the glogal class in Java. It contains all
the classes with their constructors and their methods.
The lass String, like the class Integer, refers to the class object. Its constructors
and methids are redefined (overrided) regarding the ones of the supper class Object. We
do not need to use a method from the class object related to a string, it is sufficient
to use the class String.
Example:
In the folowing statement, we use the class String:
We use its constructor:
String Word1 = "Regards";
String Word2 = "All the best";
and we use its method:
Word1.length(); (which it returns the number of characters of the string Regards).
public class StringMethods {
public static void main (String [] args){
String Word1 = "Regards";
String Word2 = "All the best";
/* The two remaining braces "}}" are written at the bottom */
length of a string
1. int length () : Number of characters of a string.
System.out.println("\n\t The length of the word: " + Word1+ " is " + Word1.length());
System.out.println("\n\t The length of the string: " + Word2+ " is " + Word2.length());
Access to a character of a string
2. char charAt (int Rank) : The character in a string at the rank "Rank".
int Rank = 4;
System.out.println("\n\t The character at the rank " + Rank+ " in the string "
+ Word1 + " is " + Word1.charAt(Rank));
Conversion
3. String toUpperCase () : To convert into capital letters.
System.out.println("\n\t The string " + Word1+ " is written in lower case.\n\t
Conveted to upper case, it becomes: " + Word1.toUpperCase());
Comparison
4. String toLowerCase () : To convert into normal letters.
System.out.println("\n\t The string " + Word1.toUpperCase()+ " is written in
upper case.\n\t Conveted to lower case, it becomes: " + Word1.toUpperCase().toLowerCase());
5. boolean equals (Object Thing): Comparing two objects with true or false.
Object Thing = "Regards";
//Object Stuff = new Object ("235"); why not?
Object Stuff = "235";
if (Thing.equals(Stuff))
System.out.println("\n\t Surprizing to have " + Thing + " equal to " + Stuff);
else
System.out.println("\n\t That is normal to have " + Thing + " not equal to " + Stuff);
6. boolean equalsIgnoreCase (String Phrase): Comparing two strings with true or
false, and ignoring upper or lower cases.
String Thing1 = "REGARDS";
String Stuff1 = "regards";
if (Thing1.equalsIgnoreCase(Stuff1))
System.out.println("\n\t In this case, we have " + Thing1 + " = " + Stuff1);
else
System.out.println("\n\t We do not have " + Thing1 + " equal to " + Stuff1);
7. int compareTo (String Phrase): Comparing the length of two strings
with 0 or <0 or >0.
String Thing2 = "The best of luck";
String Stuff2 = "Regards";
if (Thing2.compareTo(Stuff2) == 0)
System.out.println("\n\t " + Thing2 + " is equal to " + Stuff2);
if (Thing2.compareTo(Stuff2) > 0)
System.out.println("\n\t " + Thing2 + " is greater than " + Stuff2);
else
System.out.println("\n\t " + Thing1 + " is less than " + Stuff1);
Searching inside a string
8.1. int indexOf (char Letter): Searching the index (the first) of a character
inside a string
String Thing3 = "All the best of luck";
char Char1 = 'b';
System.out.println("\n\t The index of the character " + Char1+ " in the string " +
Thing3 + " is " + Thing3.indexOf (Char1));
8.2. int indexOf (String phrase): Searching the index (the first) of a set of
characters inside a string
String Thing31 = "All the best of luck";
String Stuff31 = "luc";
System.out.println("\n\t The index of the set of characters " + Stuff31+ " in the
string \n\t " + Thing31 + " is " + Thing31.indexOf (Char1));
9.1. int lastIndexOf (char Letter): Searching the index of the last
specified character inside a string
String Thing4 = "All the best of luck";
char Char2 = 'l';
System.out.println("\n\t The index of the last character " + Char2+ "\n\t
in the string " + Thing4 + " is " + Thing4.lastIndexOf (Char2));
9.2 int lastIndexOf (String Phrase): Searching the index of the last set of
specified characters inside a string
String Thing41 = "At the end of the email, it is written \n\t the fragment: All
the best of luck";
String Stuff41 = "the";
System.out.println("\n\t The index of the last set of characters " + Stuff41+ "\n\t
in the string " + Thing41 + " is " + Thing41.lastIndexOf (Char2));
10. String subtring (int start, int end): Gives the set of characters inside a string
between which is relatd to the first index start (included) and which is related to the
second index end (excluded)
String Thing5 = "All the best of luck";
int start = 8;
int end = 12;
System.out.println("\n\t The part of the sentence: " + Thing5 + ",\n\t between the
inexes " + start + " and " + end + " is " + Thing5.substring (start, end));
11. String subtring (int start): Gives the set of characters inside a string from
which is relatd to the first index start (included) to the end of the string.
String Thing6 = "All the best of luck";
int beginning = 13;
System.out.println("\n\t The rest of the sentence: " + Thing6 + ",\n\t from the index "
+ beginning + " to the end of sentence is " +
Thing6.substring (start));
}
}
|