// 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);
}
}// 27. intern()
public class InternExample {
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = str1.intern();
System.out.println("Interned string: " + str2);
}
}// 28. strip()
public class StripExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String strippedString = str.strip();
System.out.println("Stripped string: '" + strippedString + "'");
}
}// 29. repeat()
public class RepeatExample {
public static void main(String[] args) {
String str = "Hello! ";
String repeatedString = str.repeat(3);
System.out.println("Repeated string: " + repeatedString);
}
}// 30. lines()
public class LinesExample {
public static void main(String[] args) {
String str = "Hello\nWorld\nWelcome";
String[] lines = str.lines().toArray(String[]::new);
System.out.println("Individual lines:");
for (String line : lines) {
System.out.println(line);
}
}
}// 31. stripLeading()
public class StripLeadingExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String strippedString = str.stripLeading();
System.out.println("Leading stripped string: '" + strippedString + "'");
}
}// 32. stripTrailing()
public class StripTrailingExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String strippedString = str.stripTrailing();
System.out.println("Trailing stripped string: '" + strippedString + "'");
}
}// 33. isBlank()
public class IsBlankExample {
public static void main(String[] args) {
String str = " ";
boolean isBlank = str.isBlank();
System.out.println("Is the string blank? " + isBlank);
}
}// 34. lines().count()
public class LinesCountExample {
public static void main(String[] args) {
String str = "Hello\nWorld\nWelcome";
long lineCount = str.lines().count();
System.out.println("Number of lines: " + lineCount);
}
}// 35. formatted()
public class FormattedExample {
public static void main(String[] args) {
String formattedString = "Name: %s, Age: %d";
String result = String.format(formattedString, "John", 30);
System.out.println("Formatted string: " + result);
}
}// 36. stripIndent()
public class StripIndentExample {
public static void main(String[] args) {
String str = " Line 1\n Line 2\n Line 3";
String strippedString = str.stripIndent();
System.out.println("Stripped indents:\n" + strippedString);
}
}// 37. translateEscapes()
public class TranslateEscapesExample {
public static void main(String[] args) {
String str = "Hello\\nWorld\\tJava";
String translatedString = str.translateEscapes();
System.out.println("Translated escapes: " + translatedString);
}
}// 38. indent(int n)
public class IndentExample {
public static void main(String[] args) {
String str = "Hello\nWorld";
String indentedString = str.indent(4);
System.out.println("Indented string:\n" + indentedString);
}
}// 39. repeat(int count)
public class RepeatCountExample {
public static void main(String[] args) {
String str = "Java ";
String repeatedString = str.repeat(3);
System.out.println("Repeated string: " + repeatedString);
}
}// 40. stripLeading(), stripTrailing()
public class StripLeadingTrailingExample {
public static void main(String[] args) {
String str = " Hello, World! ";
String strippedLeading = str.stripLeading();
String strippedTrailing = str.stripTrailing();
System.out.println("Leading stripped string: '" + strippedLeading + "'");
System.out.println("Trailing stripped string: '" + strippedTrailing + "'");
}
}// 41. replaceFirst()
public class ReplaceFirstExample {
public static void main(String[] args) {
String str = "apple orange apple orange";
String replacedString = str.replaceFirst("apple", "banana");
System.out.println("String after replacing first occurrence: " + replacedString);
}
}