String Class Examples
Strings
|
IndexOf |
Example 6 |
Array of Strings and Array of Characters
Example 1: Some Methods
public
class
Strings
{
public
static
void
main(String[ ]args)
{
String
theString = "This is
a string.";
int
len
= theString.length();
String
upperString = theString.toUpperCase();
String
lowerString = theString.toLowerCase();
char
theCharacter
= theString.charAt(4);
String
combineEm = theString + upperString;
String aPart =
theString.substring(2,9);
System.out.println("The
original string is: "
+ theString);
System.out.println("The
length of the string is "
+ theString.length());
System.out.println("The
string to lower case is "
+ lowerString);
System.out.println("The
string to upper case is "
+ upperString);
System.out.println("The
string concatenated with upper case is "
+ combineEm);
System.out.println("The
substring starting at index 2 and ending at index 8 is: "
+ aPart);
}
}
Output
The
original string is: This is a string.
The
length of the string is 17
The
string to lower case is this is a string.
The
string to upper case is THIS IS A STRING.
The
string concatenated with upper case is This is a string.THIS IS A STRING.
The substring starting at index 2 and ending at index 8 is: is is a
Example 2: Printing Using charAt and a Loop
public
class
StringExampess
{
public
static
void
main(String[] args)
{
String myString =
new
String ("This
is another string.");
for
(int
i = 0; i<myString.length();i++)
{
System.out.print(myString.charAt(i));
}
}
}
This is another string.
Example 3: Printing Shortcut
public
class
StringExamples
{
public
static
void
main(String[]
args)
{
String
myString =
new
String ("This is another string.");
System.out.print(myString);
}
Output
This is another string.
Example 4: indexOf
public
class
StringExampless
{
public
static
void
main(String[] args)
{
String myString =
new
String ("This
is another string.");
int
m = myString.indexOf('s');
//note the '' as opposed to ""
int
n = myString.indexOf('Z');
int
p = myString.indexOf("another");
System.out.println("The
index of the first s is: "
+ m);
System.out.println("The
index of the first z is: "
+ n);
System.out.print("The
index of the first character in another is: " + p);
}
}
Output
The index of the first s is: 3
The index of the first z is: -1
The index of the first character in another is: 8
public class StringExamples
{
public
static
void
main(String[] args)
{
String myString =
new
String ("This
is another string.");
char
myArray[] =
new
char[myString.length()];
for
(int
i = 0; i<myString.length(); i++)
{
myArray[i] = myString.charAt(i);
}
for
(int
j = 0; j<myString.length(); j++)
{
System.out.print(myArray[j]);
}
}
}
Output
This is another string.
Example 6
Equals versus compareTo
The equals() method evaluates the contents of two String objects to determine if they are equivalent. The method returns true if the objects have identical contents.
public
class
StringExamples
{
public
static
void
main(String[] args)
{
String aName =
"Mat";
String
anotherName = "Mat";
if
(aName.equals(anotherName))
System.out.println("The
names are the same");
else
System.out.println("The
names are not the same.");
}
}
Output
The names are the same
public
class
StringExamples
{
public
static
void
main(String[] args)
{
String aName =
"Mat";
String
anotherName =
"George";
if
(aName.equals(anotherName))
System.out.println("The
names are the same");
else
System.out.println("The
names are not the same.");
}
}
Output
The names are not the same
The compareTo method determines the alphabetical placement of two strings.
public class StringExamples
{
public
static
void
main(String[] args)
{
String nameOne = "Ezekial";
String nameTwo = "Matilda";
int
n = nameOne.compareTo(nameTwo);
if
(n < 0)
System.out.println("nameOne
comes before nameTwo");
if
(n ==0)
System.out.println("nameOne
is the same as nameTwo");
if
(n>0)
System.out.println("nameOne
comes after nameTwo");
}
}
Output
nameOne comes before nameTwo
public class StringExamples
{
public
static
void
main(String[] args)
{
String nameOne = "Ezekial";
String nameTwo = "Ezekial";
int
n = nameOne.compareTo(nameTwo);
if
(n < 0)
System.out.println("nameOne
comes before nameTwo");
if
(n ==0)
System.out.println("nameOne
is the same as nameTwo");
if
(n>0)
System.out.println("nameOne
comes after nameTwo");
}
}
Output
nameOne is the same as nameTwo
public class StringExamples
{
public
static
void
main(String[] args)
{
String nameOne = "Ezekial";
String nameTwo = "Abraham";
int
n = nameOne.compareTo(nameTwo);
if
(n < 0)
System.out.println("nameOne
comes before nameTwo");
if
(n ==0)
System.out.println("nameOne
is the same as nameTwo");
if
(n>0)
System.out.println("nameOne
comes after nameTwo");
}
}
Output
nameOne comes After nameTwo