[!INDEX]
- trim, lenght, isEmpty, equal { s1.mt() } + validation
- compair to , compairToIgnoreCase (lexiographly)
- string with +
- concat , join, Subsequence, substring, indexOf, lastIndexOf,
- syntax for class, boolean mt ( equals for object )
1. trim, lenght, isEmpty, equal { s1.mt() } + validation
package package2;
public class Main1 {
public static void main(String[] args) {
String s1 = " bull shit ";
String s2 = " Bull shit ";
String s3 = null;
System.out.println("S3 = "+s1.length()); // 2
System.out.println("S3 = "+s1.isEmpty()); // false
System.out.println("S3 = "+s1.trim()); // bull shit
System.out.println(s1.equals(s2)); // false
System.out.println(s1.equalsIgnoreCase(s2)); // true
if(s1.length()==0){
System.out.println("enter the details"); //lenght
}
if(s1.isEmpty()){
System.out.println("enter the details"); //isEmpty
}
if(s1.trim().length()==0){
System.out.println("enter the details"); //trim
}
if(s1.equals("")){
System.out.println("enter the details");//equal
}
}
}
2.compair to , compairToIgnoreCase (lexiographly)
public class Main1 {
public static void main(String[] args) {
/* compair to A=65, a=97 */
String s1 = "a"; // 97
String s2 = "A"; // 65
System.out.println(s1.compareTo(s2)); // 32
System.out.println(s2.compareTo(s1)); // -32
/* compair to A=65, a=97 */
String s3 = "abc";
String s4 = "Abc";
System.out.println(s3.compareTo(s4)); // 32
/* compair to B=66, b= 98*/
String s5 = "abc";
String s6 = "aBc";
System.out.println(s5.compareTo(s6)); // 32
/* compair to ignore case */
System.out.println(s5.compareToIgnoreCase(s6)); // 0
/* finding the lenght */
String s7 = "b";
String s8 = "";
System.out.println(s7.compareTo(s8)); // 1, always
/* chart link https://www.w3schools.com/charsets/ref_html_ascii.asp */
}
}
3. string with +
package package2;
public class Main1 {
public static void main(String[] args) {
String s1 = "- - -";
String s2 = "CCC";
String s3 ="1234567890";
String s4 ="12345 12345";
/* + mt */
System.out.println(s1+s2); //- - -CCC
System.out.println(s1+10); //- - -10
System.out.println(s1+10+20); //- - -1020
System.out.println(10+20+s1); //30- - -
System.out.println(10+s1+20); //10- - -20
System.out.println(s1+20/10); //- - -2
System.out.println(s1+20*10); //- - -200
System.out.println(s1+10-5); // java.lang.Error: Unresolved compilation problem
/* concat */
}
}
4. concat , join, Subsequence, substring, indexOf, lastIndexOf,
package package2;
public class Main1 {
public static void main(String[] args) {
String s1 = "- - -";
String s2 = "CCC";
String s3 ="1234567890";
String s4 ="12345 12345";
System.out.println(s1.concat(s2)); //- - -CCC
/* join m/T, static m/T,JDK 1.8, delimiter is null -> java.lang.NullPointerException*/
System.out.println(String.join(",",s1,s2)); // - - -,CCC
/* subSequence , return character */
System.out.println(s3.subSequence(2, 4)); // 34
System.out.println(s1.subSequence(0, -1)); // outof bound
System.out.println(s3.subSequence(0, 0)); //
/* subSequence , return string */
System.out.println(s4.substring(3)); // 45 12345
System.out.println(s4.substring(2,6)); // 345
/* indexOf() */
System.out.println(s4.indexOf('2')); // 1
/* lastindexOf() */
System.out.println(s4.lastIndexOf('2')); // 7
}
}
5. replace , replaceFirst, replaceAll
package package2;
public class Main1 {
public static void main(String[] args) {
String s1 = "- - -";
String s2 = "CCC";
String s3 ="1234567890";
String s4 ="12345 12345";
/* replace , return string */
System.out.println(s4.replace("23", "XY")); // 1XY45 1XY45
/* replaceFirst , return string */
System.out.println(s4.replaceFirst("23", "--")); // 1--45 12345
System.out.println(s4.replaceAll("23", "**")); // 1**45 1**45
/* replaceall with regex */
System.out.println(s4.replaceAll("3(.)", "#")); // 12#5 12#5
System.out.println(s4.replaceAll("4(.*)", "^")); // 123^
}
}
6. charAt , contains, startsWith, endsWith
package package2;
public class Main1 {
public static void main(String[] args) {
String s1 = "- - -";
String s2 = "CCC";
String s3 ="1234567890";
String s4 ="12345 12345";
/* charAt() */
System.out.println(s4.charAt(2)); // 3
/* contains() */
System.out.println(s4.contains("3")); // true
/* startsWith() */
System.out.println(s4.startsWith("1")); // true
/* endswith() */
System.out.println(s4.endsWith("5")); // true
}
}
7. toUpperCase , toLowerCase, toCharArray, valueOf
package package2;
public class Main1 {
public static void main(String[] args) {
/* toupperCase() */
String s5 = "momoj";
String s6 = "KAPIL";
String s7 = "meerut";
System.out.println(s5.toUpperCase());
/* toLowerCase */
System.out.println(s6.toLowerCase());
/* valueOf */
int a =10;
String x = String.valueOf(a); // static m/T
System.out.println(x); //10
/* toCharArray */
char[]ss = s7.toCharArray(); // string to character conversion
System.out.println(ss); // meerut
System.out.println(ss[1]); // e
}
}