如何将字符串格式化为一行,StringUtils?

发布于 2024-09-09 11:44:05 字数 164 浏览 1 评论 0原文

我有一个字符串,我将其传递给 log4j 以将其记录到文件中,该字符串的内容是 XML,并且它被格式化为带有缩进等的多行,以使其易于阅读。

但是,我希望 XML 全部在一行上,我该怎么做呢?我看过 StringUtils,我想我可以去掉制表符和回车符,但一定有更干净的方法吗?

谢谢

I have a String that I'm passing to log4j to have it logged to a file, the contents of that string is XML, and it is formatted to be on multiple lines with indentations and so forth, to make it easy to read.

However, I would like that XML to be all on one line, how can I do this? I've had a look at StringUtils, I guess I could strip the tabs and carriage returns, but there must be a cleaner way?

Thanks

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

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

发布评论

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

评论(4

我的奇迹 2024-09-16 11:44:07

我会对其进行正则表达式替换。虽然效率不高,但肯定比 XML 解析快!

这是未经测试的:

 String cleaned = original.replaceAll("\\s*[\\r\\n]+\\s*", "").trim();

如果我没有犯错,这将消除所有行终止符以及紧随这些行终止符之后的任何空格。模式开头的空白应消除各行上的任何尾随空白。 trim() 的引入是为了消除第一行开头和最后一行结尾处的空格。

I'd throw a regexp replace on it. That's not highly efficient but sure to be faster than XML parsing!

This is untested:

 String cleaned = original.replaceAll("\\s*[\\r\\n]+\\s*", "").trim();

If I haven't goofed, that will eliminate all line terminators as well as any whitespace immediately following those line terminators. The whitespace at the beginning of the pattern should kill any trailing whitespace on individual lines. trim() is thrown in for good measure to eliminate whitespace at the start of the first line and the end of the last.

我们只是彼此的过ke 2024-09-16 11:44:07

也许与 JDom http://www.jdom.org/

public static Document createFromString(final String xml) {
    try {
        return new SAXBuilder().build(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    } catch (JDOMException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

public static String renderRaw(final Document description) {
    return renderDocument(description, getRawFormat());
}

public static String renderDocument(final Document description, final Format format) {
    return new XMLOutputter(format).outputString(description);
}

Perhaps with JDom http://www.jdom.org/

public static Document createFromString(final String xml) {
    try {
        return new SAXBuilder().build(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    } catch (JDOMException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

public static String renderRaw(final Document description) {
    return renderDocument(description, getRawFormat());
}

public static String renderDocument(final Document description, final Format format) {
    return new XMLOutputter(format).outputString(description);
}
蓝眼睛不忧郁 2024-09-16 11:44:07
String oneline(String multiline) {
    String[] lines = multiline.split(System.getProperty("line.separator"));
    StringBuilder builder = new StringBuilder();
    builder.ensureCapacity(multiline.length()); // prevent resizing
    for(String line : lines) builder.append(line);
    return builder.toString();
}
String oneline(String multiline) {
    String[] lines = multiline.split(System.getProperty("line.separator"));
    StringBuilder builder = new StringBuilder();
    builder.ensureCapacity(multiline.length()); // prevent resizing
    for(String line : lines) builder.append(line);
    return builder.toString();
}
云雾 2024-09-16 11:44:07

@corsiKa 的答案,但使用 Java 8+ 可选。另外,我发现使用行分隔符系统属性不能作为 String.split 的正则表达式。

Optional.ofNullable(multilineString)
    .map(s -> s.split("[\\r\\n]+"))
    .stream()
    .flatMap(Arrays::stream)
    .map(String::trim)
    .reduce("", (accumulator, current) -> accumulator + current);

@corsiKa's answer, but using Java 8+ Optional. Also, I found that using the line separator system property didn't work as a regex to String.split.

Optional.ofNullable(multilineString)
    .map(s -> s.split("[\\r\\n]+"))
    .stream()
    .flatMap(Arrays::stream)
    .map(String::trim)
    .reduce("", (accumulator, current) -> accumulator + current);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文