strings :: string methods
// Strings are declared with statements such as:
String myName = "Eve";
String newName = new String ("Eve"); //create an object
// Breaking Strings Apart
// first position starts at 0 so last character will be in
// position length - 1.
String obj1 = "agnes";
//length() assigns the # of characters of obj1 to an int variable
int obj1Length = obj1.length ();
System.out.println (obj1.length ());
String word = "computer";
int w1 = word.length (); //returns integer 8
System.out.println (w1);
String w2 = word.substring (5);//character 5 to the end of word.
System.out.println (w2);
String w3 = word.substring (4, 7);//characters 4, 5 and 6
System.out.println (w3);
/* Joining or Concatenating Strings
If word1 = "word" and word2 = "processing" and word3 = word1 + word2;
then word3 is "wordprocessing", the strings are joined together
in the order specified with no space between then */
String obj2 = "Real ";
String obj3 = "Madrid";
String obj4 = obj2 + obj3;
String obj5= "FC ";
String club = obj5.concat (obj4);
System.out.println (club);
the method replace would replace all occurrences
of the first character by the second */
String obj6 = "oregano";
String obj7 = obj6.replace ('o', 'a');
System.out.println (obj7);
String word7 = "Computer World";
String word8 = word7.toUpperCase ();// "COMPUTER WORLD"
word7 = word8.toLowerCase ();// "computer world"
//Searching for Characters and String within Strings
String obj8= "Karolina";
int position = obj1.indexOf ("oli");
System.out.println (position);
// returns 3, the location of the o in karolina
int position2 = obj1.indexOf ((int) 'l');// karolina
System.out.println (position2);
/* Note that the cast integer (int) must be used with a single character.
to cast is to force a conversion from one data type to another
The charAt() method will return the character in the position specified.
Make sure to declare the variable as a 'char' */
String student = "Marcel";
for (int i = 0 ; i < student.length () ; i++)
{
System.out.println (student.charAt (i));
}
char position5 = student.charAt (4);
System.out.println (position5 ); // e
int position6 = student.charAt (4);
System.out.println (position6); // 101
String s1 = "yes";
String s3= new String ("yes");
if (s1==s3) // not a reference to the same object
System.out.println("true");
//reference to the same object
String s2 = s3;
if (s2==s3)
System.out.println("true");