重新格式化 Java 字符串

发布于 2024-09-01 08:47:02 字数 235 浏览 4 评论 0原文

我有一个看起来像这样的字符串:

CALDARI_STARSHIP_ENGINEERING

我需要将其编辑为看起来像

Caldari Starship Engineering

不幸的是现在是凌晨三点,我一生都无法弄清楚这一点。我在替换字符串中的内容时总是遇到麻烦,所以任何帮助都会很棒,并且会帮助我了解将来如何做到这一点。

I have a string that looks like this:

CALDARI_STARSHIP_ENGINEERING

and I need to edit it to look like

Caldari Starship Engineering

Unfortunately it's three in the morning and I cannot for the life of me figure this out. I've always had trouble with replacing stuff in strings so any help would be awesome and would help me understand how to do this in the future.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尴尬癌患者 2024-09-08 08:47:02

像这样的事情很简单:

    String text = "CALDARI_STARSHIP_ENGINEERING";
    text = text.replace("_", " ");
    StringBuilder out = new StringBuilder();
    for (String s : text.split("\\b")) {
        if (!s.isEmpty()) {
            out.append(s.substring(0, 1) + s.substring(1).toLowerCase());
        }
    }
    System.out.println("[" + out.toString() + "]");
    // prints "[Caldari Starship Engineering]"

在单词边界锚点上进行分割

另请参阅


匹配器循环解决方案

如果您不介意使用StringBuffer,您还可以使用Matcher.appendReplacement/Tail循环,如下所示:

    String text = "CALDARI_STARSHIP_ENGINEERING";
    text = text.replace("_", " ");

    Matcher m = Pattern.compile("(?<=\\b\\w)\\w+").matcher(text);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, m.group().toLowerCase());
    }
    m.appendTail(sb);
    System.out.println("[" + sb.toString() + "]");
    // prints "[Caldari Starship Engineering]"

正则表达式使用断言来匹配“尾部”部分一个单词,需要小写的部分。它查看 (?<=...) 后面,发现有一个单词边界 \b,后面跟着一个单词字符 \w。任何剩余的 \w+ 都需要匹配,以便可以小写。

相关问题

Something like this is simple enough:

    String text = "CALDARI_STARSHIP_ENGINEERING";
    text = text.replace("_", " ");
    StringBuilder out = new StringBuilder();
    for (String s : text.split("\\b")) {
        if (!s.isEmpty()) {
            out.append(s.substring(0, 1) + s.substring(1).toLowerCase());
        }
    }
    System.out.println("[" + out.toString() + "]");
    // prints "[Caldari Starship Engineering]"

This split on the word boundary anchor.

See also


Matcher loop solution

If you don't mind using StringBuffer, you can also use Matcher.appendReplacement/Tail loop like this:

    String text = "CALDARI_STARSHIP_ENGINEERING";
    text = text.replace("_", " ");

    Matcher m = Pattern.compile("(?<=\\b\\w)\\w+").matcher(text);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, m.group().toLowerCase());
    }
    m.appendTail(sb);
    System.out.println("[" + sb.toString() + "]");
    // prints "[Caldari Starship Engineering]"

The regex uses assertion to match the "tail" part of a word, the portion that needs to be lowercased. It looks behind (?<=...) to see that there's a word boundary \b followed by a word character \w. Any remaining \w+ would then need to be matched so it can be lowercased.

Related questions

猫腻 2024-09-08 08:47:02

您可以尝试以下操作:

String originalString = "CALDARI_STARSHIP_ENGINEERING";
String newString =
    WordUtils.capitalize(originalString.replace('_', ' ').toLowerCase());

WordUtils 是 Commons Lang 库的一部分 (http:// commons.apache.org/lang/

You can try this:

String originalString = "CALDARI_STARSHIP_ENGINEERING";
String newString =
    WordUtils.capitalize(originalString.replace('_', ' ').toLowerCase());

WordUtils are part of the Commons Lang libraries (http://commons.apache.org/lang/)

Spring初心 2024-09-08 08:47:02

使用正则表达式:

String s = "CALDARI_STARSHIP_ENGINEERING";
StringBuilder camel = new StringBuilder();
Matcher m = Pattern.compile("([^_])([^_]*)").matcher(s);
while (m.find())
    camel.append(m.group(1)).append(m.group(2).toLowerCase());

Using reg-exps:

String s = "CALDARI_STARSHIP_ENGINEERING";
StringBuilder camel = new StringBuilder();
Matcher m = Pattern.compile("([^_])([^_]*)").matcher(s);
while (m.find())
    camel.append(m.group(1)).append(m.group(2).toLowerCase());
浅浅淡淡 2024-09-08 08:47:02

未经测试,但这就是我前段时间实现的方法:

s = "CALDARI_STARSHIP_ENGINEERING";
StringBuilder b = new StringBuilder();
boolean upper = true;
for(char c : s.toCharArray()) {
    if( upper ) {
        b.append(c);
        upper = false;
    } else if( c = '_' ) {
        b.append(" ");
        upper = true;
    } else {
        b.append(Character.toLowerCase(c));
    }
}
s = b.toString();

请注意,EVE 许可协议可能禁止编写对您的职业生涯有帮助的外部工具。这可能会成为你学习 Python 的触发点,因为 EVE 的大部分都是用 Python 编写的:)。

Untested, but thats how I implemented the same some time ago:

s = "CALDARI_STARSHIP_ENGINEERING";
StringBuilder b = new StringBuilder();
boolean upper = true;
for(char c : s.toCharArray()) {
    if( upper ) {
        b.append(c);
        upper = false;
    } else if( c = '_' ) {
        b.append(" ");
        upper = true;
    } else {
        b.append(Character.toLowerCase(c));
    }
}
s = b.toString();

Please note that the EVE license agreements might forbit writing external tools that help you in your careers. And it might be the trigger for you to learn Python, because most of EVE is written in Python :).

如若梦似彩虹 2024-09-08 08:47:02

快速而肮脏的方法:

全部

   line.toLowerCase();

小写拆分成单词:

   String[] words = line.split("_");

然后循环遍历第一个字母大写的单词:

  words[i].substring(0, 1).toUpperCase() 

Quick and dirty way:

Lower case all

   line.toLowerCase();

Split into words:

   String[] words = line.split("_");

Then loop through words capitalising first letter:

  words[i].substring(0, 1).toUpperCase() 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文