Oops With Java (Autosaved)
Oops With Java (Autosaved)
• Class/Static Variables:
• Scope: Throughout the class.
• Lifetime: Until the end of the program.
• Local Variables:
• Scope: Within the block it is declared.
• Lifetime: Until control leaves the block in which it is declared.
Strings: Methods
• 1. compareToIgnoreCase()
public class Main {
public static void main(String[] args) {
String myStr1 = "HELLO";
String myStr2 = "hello";
System.out.println(myStr1.compareToIgnoreCase(myStr2));
}
}
• 2. compareTo()
public class Main {
public static void main(String[] args) {
String myStr1 = "Hello";
String myStr2 = "Hello";
System.out.println(myStr1.compareTo(myStr2)); // Returns 0 because they are equal
}
}
• trim()
public class Main {
public static void main(String[] args) {
String myStr = " Hello World! ";
System.out.println(myStr);
System.out.println(myStr.trim());
}
}
• substring()
public class Main {
public static void main(String[] args) {
String myStr = "Hello, World!";
System.out.println(myStr.substring(7, 12));
}
}
• indexOf()
public class Main {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.indexOf("planet"));
}
}
• lastIndexOf()
public class Main {
public static void main(String[] args) {
String myStr = "Hello planet earth, you are a great planet.";
System.out.println(myStr.lastIndexOf("planet"));
}
}
• replace()
public class Main {
public static void main(String[] args) {
String myStr = "Hello";
System.out.println(myStr.replace('l', 'p'));
}
}
• subSequence()
public class Main {
public static void main(String[] args) {
String myStr = "Hello, World!";
System.out.println(myStr.subSequence(7, 12));
}
}