������
������Java ������������������������������������������������������������������������������ ���������String ������������������������������������������������������������������������������������������������
- repeat(int count)���������������������������������������������������������������������������������������������
- isBlank()������������������������������������������������������������ 0 ���������������������������������������
- lines()������������������������������������������������������������
- strip()������������������������������������������������������������������������������������������������������������
- stripLeading()���������������������������������������������������������������������������������������������
- stripTrailing()���������������������������������������������������������������������������������������������
- formatted(Object... args)���������������������������������������������������������������������������������
- translateEscapes()������ Java ������������������������������������������������������������������������
- transform() ���������������������������������������������������������������������
������
1. repeat(int count)
public class StringRepeatExample {
public static void main(String[] args) {
String str = "abc";
String repeatedStr = str.repeat(3);
System.out.println(repeatedStr);
}
}
������������
复制
���������������
abcabcabc ������������
复制
2. isBlank()
public class StringIsBlankExample {
public static void main(String[] args) {
String str1 = "";
String str2 = " ";
String str3 = " \t ";
System.out.println(str1.isBlank());
System.out.println(str2.isBlank());
System.out.println(str3.isBlank());
}
}
������������
复制
���������������
true
true
true
������������
复制
3. lines()
import java.util.stream.Stream;
public class StringLinesExample {
public static void main(String[] args) {
String str = "Hello\nWorld\nJava";
Stream<String> lines = str.lines();
lines.forEach(System.out::println);
}
}
������������
复制
���������������
Hello World Java ������������
复制
4. strip()
public class StringStripExample {
public static void main(String[] args) {
String str1 = " abc ";
String str2 = "\t def \n";
System.out.println(str1.strip());
System.out.println(str2.strip());
}
}
������������
复制
���������������
abc def ������������
复制
5. stripLeading()
public class StringStripLeadingExample {
public static void main(String[] args) {
String str1 = " abc ";
String str2 = "\t def \n";
System.out.println(str1.stripLeading());
System.out.println(str2.stripLeading());
}
}
������������
复制
���������������
abc def ������������
复制
6. stripTrailing()
public class StringStripTrailingExample {
public static void main(String[] args) {
String str1 = " abc ";
String str2 = "\t def \n";
System.out.println(str1.stripTrailing());
System.out.println(str2.stripTrailing());
}
}
������������
复制
���������������
abc def ������������
复制
7. formatted(Object... args)
public class StringFormattedExample {
public static void main(String[] args) {
String str = "My name is %s, I'm %d years old.";
String formattedStr = str.formatted( "John", 25);
System.out.println(formattedStr);
}
}
������������
复制
���������������
My name is John, I'm 25 years old. ������������
复制
8. translateEscapes()
public class StringTranslateEscapesExample {
public static void main(String[] args) {
String str = "Hello\\nWorld\\tJava";
String translatedStr = str.translateEscapes();
System.out.println(translatedStr);
}
}
������������
复制
���������������
Hello World Java ������������
复制
9. transform()
public class StringTransformExample {
public static void main(String[] args) {
String str = "hello world";
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
String newStr = new String(bytes, StandardCharsets.ISO_8859_1);
System.out.println(newStr);
}
}
������������
复制
���������������
hello world ������������
复制
��������������������������������������������� "hello world"
��� UTF-8 ��������������� ISO-8859-1 ���������������������������������������������������������������������������������������������������������������������������������������������������������������������
������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������