ch
Feedback
Java Codes Basic to Advance

Java Codes Basic to Advance

前往频道在 Telegram
2 333
订阅者
无数据24 小时
-37
-2030
帖子存档
// 26. offsetByCodePoints()
public class OffsetByCodePointsExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int index = str.offsetByCodePoints(0, 7);
        System.out.println("Index after moving 7 code points forward: " + index);
    }
}

// 25. codePointCount()
public class CodePointCountExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int codePoints = str.codePointCount(0, str.length());
        System.out.println("Number of Unicode code points: " + codePoints);
    }
}

// 24. codePointBefore()
public class CodePointBeforeExample {
    public static void main(String[] args) {
        String str = "Hello";
        int codePoint = str.codePointBefore(3);
        System.out.println("Code point before index 3: " + codePoint);
    }
}

// 23. codePointAt()
public class CodePointAtExample {
    public static void main(String[] args) {
        String str = "Hello";
        int codePoint = str.codePointAt(1);
        System.out.println("Code point at index 1: " + codePoint);
    }
}

// 22. valueOf()
public class ValueOfExample {
    public static void main(String[] args) {
        int number = 42;
        String convertedNumber = String.valueOf(number);
        System.out.println("Converted number to string: " + convertedNumber);
    }
}

// 21. matches()
public class MatchesExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean matches = str.matches("^[a-zA-Z0-9]*$");
        System.out.println("Matches the pattern? " + matches);
    }
}

// 20. replaceAll()
public class ReplaceAllExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String replacedString = str.replaceAll("[aeiou]", "*");
        System.out.println("String after replacement: " + replacedString);
    }
}

// 19. substring(int beginIndex, int endIndex)
public class SubstringWithIndicesExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String substring = str.substring(7, 12);
        System.out.println("Substring from index 7 to 12: " + substring);
    }
}

// 18. lastIndexOf()
public class LastIndexOfExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        int lastIndex = str.lastIndexOf('o');
        System.out.println("Last index of 'o': " + lastIndex);
    }
}

// 17. equalsIgnoreCase()
public class EqualsIgnoreCaseExample {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "HELLO";
        boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
        System.out.println("Are strings equal ignoring case? " + isEqualIgnoreCase);
    }
}

// 16. concat()
public class ConcatExample {
    public static void main(String[] args) {
        String str1 = "Hello, ";
        String str2 = "World!";
        String concatenatedString = str1.concat(str2);
        System.out.println("Concatenated string: " + concatenatedString);
    }
}

// 15. split()
public class SplitExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String[] parts = str.split(", ");
        System.out.println("Split string: " + Arrays.toString(parts));
    }
}

// 14. toCharArray()
public class ToCharArrayExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        char[] charArray = str.toCharArray();
        System.out.println("Character array: " + Arrays.toString(charArray));
    }
}

// 13. charAt()
public class CharAtExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        char character = str.charAt(4);
        System.out.println("Character at index 4: " + character);
    }
}

// 12. endsWith()
public class EndsWithExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean endsWith = str.endsWith("World!");
        System.out.println("Does the string end with 'World!'? " + endsWith);
    }
}

// 11. startsWith()
public class StartsWithExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean startsWith = str.startsWith("Hello");
        System.out.println("Does the string start with 'Hello'? " + startsWith);
    }
}

// 10. isEmpty()
public class IsEmptyExample {
    public static void main(String[] args) {
        String emptyString = "";
        boolean isEmpty = emptyString.isEmpty();
        System.out.println("Is the string empty? " + isEmpty);
    }
}

// 9. trim()
public class TrimExample {
    public static void main(String[] args) {
        String spacedString = "   Some spaces   ";
        String trimmedString = spacedString.trim();
        System.out.println("Trimmed string: '" + trimmedString + "'");
    }
}

// 8. contains()
public class ContainsExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        boolean contains = str.contains("World");
        System.out.println("Does the string contain 'World'? " + contains);
    }
}

// 7. replace()
public class ReplaceExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String replacedString = str.replace('l', 'z');
        System.out.println("String after replacement: " + replacedString);
    }
}