简体   繁体   English

System.out.println为什么打印新行而System.out.print什么都不打印?

[英]Why does System.out.println print a new line while System.out.print prints nothing?

I am having trouble with split function. 我在拆分功能方面遇到了麻烦。 I do not know how it works. 我不知道它是如何工作的。

Here is my source code: 这是我的源代码:

// Using println to print out the result
String str  = "         Welcome       to    Java     Tutorial     ";
str = str.trim();
String[] arr = str.split(" ");
for (String c : arr) {
    System.out.println(c);
}

//Using print to  print out the result
String str  = "         Welcome       to    Java     Tutorial     ";
str = str.trim();
String[] arr = str.split(" ");
for (String c : arr) {
    System.out.print(c);
}

and the results are: 结果是: 结果

The first is the result when using println , the second is the result when using print , 第一个是使用println时的结果,第二个是使用print时的结果,

I do not understand why space appears in println , while it does not appear in print . 我不明白为什么在println出现空格,而它没有出现在print Can anyone explain it for me? 有谁可以帮我解释一下?

Since you have many spaces in your string, if you look at the output of split function, the resulted array looks like 由于字符串中有许多空格,如果查看split函数的输出,结果数组看起来就像

[Welcome, , , , , , , to, , , , Java, , , , , Tutorial]

So if you look close they are empty String's "" . 因此,如果你仔细看看它们是空的String's ""

When you do a println("") it is printing a line with no string. 当你执行println("")它会打印一个没有字符串的行。

However when you do print("") , it is no more visibility of that string and it looks nothing getting printed. 但是当你print("") ,它不再是该字符串的可见性,它看起来没有任何打印。

If you want to separate them regardless of spaces between them, split them by white space. 如果要将它们分开,无论它们之间是否有空格,请用空格分隔它们。

Lastly, trim() won't remove the spaces within the String. 最后,trim()不会删除String中的空格。 It can only trim spaces in the first and last. 它只能修剪第一个和最后一个空格。

What you are doing is splitting by every individual white space. 你正在做的是分裂每个单独的空白区域。 So for every space you have in your string, it is split as a separate string If you want to split just the words, you can use the whitespace regex: 因此,对于字符串中的每个空间,它将被拆分为单独的字符串如果要仅拆分单词,可以使用空白正则表达式:

String[] arr = str.split("\\s+");

This will fix your problem with the consecutive whitespaces you are printing. 这将解决您正在打印的连续空格的问题。

Also, When you use print instead of println your print value DOES NOT carry over to the next line. 此外,当您使用print而不是println您的打印值不会延续到下一行。 Thus when you cann println("") you are just going to a new line. 因此,当你使用println("")时,你只需要换行。

println() method prints everything in a new line but the print() method prints everything in the same line. println()方法在新行中打印所有内容,但print()方法在同一行中打印所有内容。

So, when you are splitting your string by space(" ") then, for the first one, the string is splitting for every space. 因此,当您按空格(“”)拆分字符串时,对于第一个字符串,字符串将为每个空格分割。 So, every time a new line comes while printing. 因此,每次打印时都会出现新行。 You can't see anything just the new line because you are printing: "" this. 你不能看到任何新行,因为你正在打印: ""这个。 Because your code is: 因为您的代码是:

System.out.println("");

But for the second one, the string is splitting for every space but you are using print() method that's why it's not going to the new line. 但是对于第二个字符串,字符串是为每个空格分割,但是你使用的是print()方法,这就是为什么它不会进入新行。

You can overcome this by regex. 你可以用正则表达式克服这一点。 You can use this regex: \\\\s+ 你可以使用这个正则表达式: \\\\s+

So, you have to write: 所以,你必须写:

String[] arr = str.split("\\s+");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM