Introduction to Java
Jim White
Java Developer
String
= the length of the String
String.length()
returns int
for number of charactersString userName = "JSmith13";
int userNameLength = userName.length(); // Value is 8
.toLowerCase()
class StringMethods1 {
public static void main (String[] args){
String yes = "Yes";
System.out.println(yes.toLowerCase());
}
}
yes
.toUpperCase()
class StringMethods2 {
public static void main (String[] args){
String yes = "Yes";
System.out.println(yes.toUpperCase());
}
}
YES
+
to combine two String
s togetherclass StringMethodsCont {
public static void main (String[] args){
String message1 = "Java is";
String message2 = "awesome";
// Use " " to nicely format the final message
System.out.println(message1 + " " + message2);
}
}
Java is awesome
+
automatically converts numbers to String
when used with a String
class StringMethodsCont {
public static void main (String[] args){
String message1 = "Java is";
int message2 = 29;
// Use " " to nicely format the final message
System.out.println(message1 + " " + message2);
}
}
Java is 29
// Define now String courseCreators;
// Assign value later courseCreators = "Instructor: Jim White; Collaborators: Kat, George, Arne, Eduardo";
String
= collection of characters in double quotes, with built-in functionality// Declare now
String testMessage;
// Assign later
testMessage = "This is a test";
// Method to return the String's length (number of characters)
int msgLength = testMessage.length(); // Value is 14
// Methods to convert String to lower or upper case
String lowerMessage = testMessage.toLowerCase(); // Value is "this is a test"
String upperMessage = testMessage.toUpperCase(); // Value is "THIS IS A TEST"
Introduction to Java