暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

告别StringUtil:使用Java 全新String API优化你的代码

原创 apq 2023-05-18
75

������

������Java ������������������������������������������������������������������������������ ���������String ������������������������������������������������������������������������������������������������

  1. repeat(int count)���������������������������������������������������������������������������������������������
  2. isBlank()������������������������������������������������������������ 0 ���������������������������������������
  3. lines()������������������������������������������������������������
  4. strip()������������������������������������������������������������������������������������������������������������
  5. stripLeading()���������������������������������������������������������������������������������������������
  6. stripTrailing()���������������������������������������������������������������������������������������������
  7. formatted(Object... args)���������������������������������������������������������������������������������
  8. translateEscapes()������ Java ������������������������������������������������������������������������
  9. 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 ���������������������������������������������������������������������������������������������������������������������������������������������������������������������

������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论