黑马程序员技术交流社区

标题: java [打印本页]

作者: 凝聚    时间: 2013-10-1 20:53
标题: java
本帖最后由 杨增坤 于 2013-10-1 21:28 编辑

怎样在字符串结尾添加换行符,输出完字符串后换一行输出其他内容。

作者: 杨增坤    时间: 2013-10-1 21:20
由于不同的系统换行标志不一样,现在以\n为例。

System.out.println();
system.out.print("\n");
都表示换行!

作者: 凝聚    时间: 2013-10-1 21:27
杨增坤 发表于 2013-10-1 21:20
由于不同的系统换行标志不一样,现在以\n为例。

System.out.println();

谢谢解决了,我又从网上找到这么一个方法:+System.getProperty("line.separator")。

作者: 赖龙威    时间: 2013-10-1 21:28
System.out.println();
或者“……\n”
作者: 凝聚    时间: 2013-10-1 21:36
杨增坤 发表于 2013-10-1 21:20
由于不同的系统换行标志不一样,现在以\n为例。

System.out.println();

package twentyfive;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class five2 {

        public static void main(String[] args) {
                getDemo2();

        }
public static void getDemo2()
{
//String str="i love china  chian success forever  four five"+System.getProperty("line.separator");
        String str="i love china  chian success forever  four five";
        System.out.print("\n");
System.out.print(str);
String reg="\\b[a-z]{4}\\b";
Pattern p=Pattern.compile(reg);
Matcher m=p.matcher(str);
while(m.find())
{
        System.out.println(m.group());
        System.out.println(m.start()+"....."+m.end());
}
}
}
这段程序输出结果为:
i love china  chian success forever  four fivelove
2.....6
four
37.....41
five
42.....46

应该love这个字符在2.....6上面的而不是字符串结尾,而System.getProperty("line.separator")这个方法解决了这个问题输出为:
i love china  chian success forever  four five
love
2.....6
four
37.....41
five
42.....46








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2