// 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);
}
}❤1
// 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);
}
}// 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 + "'");
}
}// 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);
}
}// 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);
}
}// 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);
}
}👍1
// 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);
}
}// 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));
}
}// 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));
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}// 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);
}
}