从java中的字符串修剪新行字符 [英] Trimming new line character from a string in java

查看:106
本文介绍了从java中的字符串修剪新行字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序的输出:

public class TestClass {

    public static void main(final String[] args){
        String token = "null\n";
        token.trim();
        System.out.println("*");
        System.out.println(token);
        System.out.println("*");
    }
}

是:

*
null

*

然而

如何删除新行字符串的开头和结尾(Java)?

另有说法。

我缺少什么?

推荐答案

String 是不可变的

token.trim();

不会更改基础值,它会返回一个新的字符串没有前导和结尾的空白字符。您需要替换您的参考

doesn't change the underlying value, it returns a new String without the leading and ending whitespace characters. You need to replace your reference

token = token.trim();

这篇关于从java中的字符串修剪新行字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆