如何用回车创建一个String? [英] How to create a String with carriage returns?

查看:126
本文介绍了如何用回车创建一个String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于JUnit测试,我需要一个由多行组成的String。但我得到的只是一个单行字符串。我尝试了以下方法:

For a JUnit test I need a String which consists of multiple lines. But all I get is a single lined String. I tried the following:

    String str = ";;;;;;\n" +
                 "Name, number, address;;;;;;\n" + 
                 "01.01.12-16.02.12;;;;;;\n" + 
                 ";;;;;;\n" + 
                 ";;;;;;";

我也试过 \ n\\\ 而不是 \ n 。 System.getProperty(line.separator)也不起作用。它在String中产生 \ n 并且没有回车符。那我怎么解决呢?

I also tried \n\r instead of \n. System.getProperty("line.separator") doesn't work too. it produces a \n in String and no carriage return. So how can I solve that?

推荐答案

感谢您的回答。我错过了我的数据存储在 List< String> 中,并传递给测试方法。错误是我将字符串放入ArrayList的第一个元素。这就是为什么我认为String只包含一行,因为调试器只显示了一个条目。

Thanks for your answers. I missed that my data is stored in a List<String> which is passed to the tested method. The mistake was that I put the string into the first element of the ArrayList. That's why I thought the String consists of just one single line, because the debugger showed me only one entry.

这篇关于如何用回车创建一个String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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