Cách sử dụng đối tượng String

Trong post này:

  • Lấy độ dài String
  • So sánh String
  • Conver String thành Characters
  • Tìm kiếm trong String
  • Tìm vi trí xâu trong String
  • Cắt String
  • Convert String
  • Replacing xâu trong String
  • Swtich String
  • Switch Compare String

1. Lấy độ dài String

public class Main {
  public static void main(String[] args) {
    String str1 = new String();
    String str2 = new String("Hello");
    int len1 = str1.length();
    int len2 = str2.length();

    // Display the length of str1 and str2
    System.out.println("Length of \"" + str1 + "\" = " + len1);
    System.out.println("Length of \"" + str2 + "\" = " + len2);
  }
}

2. So sánh String

public class Main {
  public static void main(String[] args) {
    String apple = new String("Apple");
    String orange = new String("Orange");
    System.out.println(apple.equals(orange));
    System.out.println(apple.equals(apple));
    System.out.println(apple == apple);
    System.out.println(apple == orange);
    System.out.println(apple.compareTo(apple));
    System.out.println(apple.compareTo(orange));
  }
}

3. Convert String thành Characters

public class Main {
  public static void main(String[] args) {
    String str = "DINHTHEHIEN.COM";
    int len = str.length();

    // Loop through all characters and print their indexes
    for (int i = 0; i < len; i++) {
      System.out.println(str.charAt(i) + " has index " + i);
    }
  }
}

4. Tìm kiếm trong String

public class Main {
  public static void main(String[] args) {
    String str = new String("Apple");
    int index = str.indexOf('p'); // index will have a value of 1
    System.out.println(index);

    index = str.indexOf("pl"); // index will have a value of 2
    System.out.println(index);
    index = str.lastIndexOf('p'); // index will have a value of 2
    System.out.println(index);

    index = str.lastIndexOf("pl"); // index will have a value of 2
    System.out.println(index);

    index = str.indexOf("k"); // index will have a value of -1
    System.out.println(index);
  }
}

5. Tìm vi trí xâu trong String

public class Main {
  public static void main(String[] args) {
    String str = "This is a test";
    // Test str, if it starts with "This"
    if (str.startsWith("This")) {
      System.out.println("String starts with This");
    } else {
      System.out.println("String does not start with This");
    }
    // Test str, if it ends with "program"
    if (str.endsWith("program")) {
      System.out.println("String ends with program");
    } else {
      System.out.println("String does not end with program");
    }
  }
}

6. Cắt String

public class Main {
  public static void main(String[] args) {
    String str = "A,B,C,D";
    // Split str using a comma as the delimiter
    String[] parts = str.split(",");

    // Print the the string and its parts
    System.out.println(str);

    for (String part : parts) {
      System.out.println(part);
    }
  }
}

7. Convert String

public class Main {
  public static void main(String[] args) {
    String s1 = String.valueOf('C'); // s1 has "C"
    String s2 = String.valueOf("10"); // s2 has "10"
    String s3 = String.valueOf(true); // s3 has "true"
    String s4 = String.valueOf(2014); // s4 has "2014"
  }
}

8. Replacing xâu trong String

public class Main {
  public static void main(String[] args) {
    String oldStr = new String("tooth");
    String newStr = oldStr.replace('o', 'e');
    System.out.println(newStr);
  }
}

9. Swtich String

public class Main {
  public static void main(String[] args) {
    String status = "off";
    switch (status) {
      case "on":
        System.out.println("Turn on");
        break;
      case "off":
        System.out.println("Turn off");
        break;
      default:
        System.out.println("Unknown command");
        break;
    }
  }
}

10. Switch Compare String

public class Main {
  public static void main(String[] args) {
    operate("on");
    operate("off");
    operate("ON");
    operate("Nothing");
    operate("OFF");
    operate("No");
    operate("On");
    operate("OK");
    operate(null);
    operate("Yes");
  }

  public static void operate(String status) {
    // Check for null
    if (status == null) {
      System.out.println("status cannot be null.");
      return;
    }
    status = status.toLowerCase();
    switch (status) {
    case "on":
      System.out.println("Turn on");
      break;
    case "off":
      System.out.println("Turn off");
      break;
    default:
      System.out.println("Unknown command");
      break;
    }
  }
}

5 (100%) 1 vote
Click Here to Leave a Comment Below 0 comments