Java将字符串按换行符拆分为数组

huangapple go评论113阅读模式
英文:

Java split a string into an array by newline

问题

public class Test {
    public static void main(String[] args) throws IOException {
        String test = "Michael Porter Jr. 2018-19 Panini Prizm Silver Prizm PSA 10 Gem Mint! Hot! ?? \n 2018 Prizm Silver MICHAEL PORTER JR. RC PSA 10 Gem Mint";
        System.out.println(test);
        String[] test_split = test.split("\\r?\\n");
        for (String t : test_split) {
            System.out.println(t);
        }
    }
}

Your code should now correctly split the input string into an array of strings, and the loop will print each of the split strings separately.

英文:

Edit: I have modified the question for simplicity.

I have this method to split a long string into an array of strings and it shall return a double string, so that I can write it on a .csv file later:

public class Test {
    public static void main(String[] args) throws IOException {
    	String test = "Michael Porter Jr. 2018-19 Panini Prizm Silver Prizm PSA 10 Gem Mint! Hot! ?? \n 2018 Prizm Silver MICHAEL PORTER JR. RC PSA 10 Gem Mint";
    	System.out.println(test);
    	String[] test_split = test.split("\\r?\\n");
    	for (String t : test_split) {
    	    System.out.println(test_split.toString());
    	}

    }
    
}

My code returns this:

Michael Porter Jr. 2018-19 Panini Prizm Silver Prizm PSA 10 Gem Mint! Hot! ?? 
 2018 Prizm Silver MICHAEL PORTER JR. RC PSA 10 Gem Mint
[Ljava.lang.String;@4a574795
[Ljava.lang.String;@4a574795

I actually expected to have an arrays with the 2 names (split by the newline character), but I got something weird. How may I fix this please?

答案1

得分: 2

你似乎正在打印一个字符串数组,而不是一个字符串,根据您发布的输出,而不真正看到您实际调用 println 的方式是什么。如果情况确实如此,您也可以使用:

Arrays.toString(yourArray);
英文:

You seem to be printing out an Array of Strings not a String, judging by the output you posted, without really seeing in which way you actually call e.g. println.
If this is really the case you could also use:

Arrays.toString(yourArray);

答案2

得分: 1

可能更好的做法是将数组反转。将它们变成 2xN 而不是 Nx2。以下是您方法的一部分代码。

String[][] nameAndPrice = new String[2][listingName_extract.length];

for (int i = 0; i < listingName_extract.length; i++) {
    nameAndPrice[0][i] = String.valueOf(listingName_extract[i]);
    nameAndPrice[1][i] = String.valueOf(listingPrice_extract[i]);
}
return nameAndPrice;

而且您可能应该使用 Arrays.toString() 来打印数组。

System.out.println(Arrays.toString(nameAndPrice[0]));
System.out.println(Arrays.toString(nameAndPrice[1]));

然后,如果您想要将它们并排打印出来。

for (int i = 0; i < listingName_extract.length; i++) {
    System.out.println(nameAndPrice[0][i] + " " + nameAndPrice[1][i]);
}
英文:

It might be better if you reversed the arrays. Make them a 2xN instead of an Nx2 Here is a segment of your method.

         String[][] nameAndPrice = new String[2][listingName_extract.length];

		 for (int i = 0; i &lt; listingName_extract.length; i++) {
		      nameAndPrice[0][i] = String.valueOf(listingName_extract[i]);
		      nameAndPrice[1][i] = String.valueOf(listingPrice_extract[i]);
		  }
		 return nameAndPrice;

And you should probably be using Arrays.toString() to print out the array.

System.out.println(Arrays.toString(nameAndPrice[0]);
System.out.println(Arrays.toString(nameAndPrice[1]);

Then if you want to print them side by side.

 for (int i = 0; i &lt; listingName_extract.length; i++) {
     System.out.println(nameAndPrice[0][i] + &quot; &quot; + nameAndPrice[1][i]);
 }

</details>



# 答案3
**得分**: 0

根据用户TheThingAboveMe和Andreas的建议,我应该关注t(字符串),而不是test_split(数组本身)。

我还根据用户WJS的建议编辑了我的代码。

<details>
<summary>英文:</summary>

As users TheThingAboveMe and Andreas suggested, I should be looking at t (the strings) and not the test_split (the array itself).

I have also edited my code per user WJS&#39;s suggestion. 


</details>



huangapple
  • 本文由 发表于 2020年3月15日 07:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/60688320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定