用两个元素分割字符串?

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

Splitting a String by two things?

问题

  1. import java.util.Scanner;
  2. public class Chapter11_ProjectPinochle {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. String a;
  6. System.out.println("Type in your pinochle deals: ");
  7. a = sc.nextLine();
  8. sc.close();
  9. String[] deals = a.split(",");
  10. String concatenatedDeals = String.join("", deals);
  11. String[] finalDeals = concatenatedDeals.split("");
  12. }
  13. }
  1. import java.util.Scanner;
  2. public class Chapter11_ProjectPinochle {
  3. public static void main(String[] args) {
  4. Scanner sc = new Scanner(System.in);
  5. String a;
  6. System.out.println("Type in your pinochle deals: ");
  7. a = sc.nextLine();
  8. sc.close();
  9. String[] deals = a.split(",");
  10. String concatenatedDeals = String.join("", deals);
  11. String[] finalDeals = concatenatedDeals.split("");
  12. // At this point, finalDeals will contain the desired output.
  13. // You can print it to verify.
  14. System.out.print("[");
  15. for (int i = 0; i < finalDeals.length; i++) {
  16. System.out.print("\"" + finalDeals[i] + "\"");
  17. if (i < finalDeals.length - 1) {
  18. System.out.print(",");
  19. }
  20. }
  21. System.out.println("]");
  22. }
  23. }
英文:

I have the following code:

  1. import java.util.Scanner;
  2. public class Chapter11_ProjectPinochle {
  3. public static void main(String[]args) {
  4. Scanner sc=new Scanner(System.in);
  5. String a;
  6. System.out.println(&quot;Type in your pinochle deals: &quot;);
  7. a=sc.nextLine();
  8. sc.close();
  9. String[] deals=a.split(&quot;&quot;);
  10. }
  11. }

I need to split the String I named &quot;a&quot; into a 16 spaced array. But the problem with the splitting is that the input is something like this: ATKQQJ,AKQQ,KQQJN,A. I need to split this into 16 parts and save it to an array I named &quot;deals.&quot; I've tried String[] deals=a.split(&quot;&quot; &amp;&amp; &quot;,&quot;); but apparently that isn't valid. I've also tried to split String a into 2 separate arrays and then put them together, but I realized I didn't know how. I want the output to be [&quot;A&quot;,&quot;T&quot;,&quot;K&quot;,&quot;Q&quot;,&quot;Q&quot;,&quot;J&quot;,&quot;A&quot;,&quot;K&quot;,&quot;Q&quot;,&quot;Q&quot;,&quot;K&quot;,&quot;Q&quot;,&quot;Q&quot;,&quot;J&quot;,&quot;N&quot;,&quot;A&quot;] when the input is: ATKQQJ,AKQQ,KQQJN,A. How should I accomplish this?

答案1

得分: 1

  1. // 将其拆分为单个字符
  2. System.out.println(Arrays.toString("ATKQQJ,AKQQ,KQQJN,A".replace(",", "").toCharArray()));
  3. // 将其拆分为单个字符的字符串
  4. System.out.println(Arrays.toString("ATKQQJ,AKQQ,KQQJN,A".replace(",", "").split("")));
英文:
  1. //Split it out into individual characters
  2. System.out.println(Arrays.toString(&quot;ATKQQJ,AKQQ,KQQJN,A&quot;.replace(&quot;,&quot;, &quot;&quot;).toCharArray()));
  3. //Split it into strings of a single character
  4. System.out.println(Arrays.toString(&quot;ATKQQJ,AKQQ,KQQJN,A&quot;.replace(&quot;,&quot;, &quot;&quot;).split(&quot;&quot;)));

答案2

得分: 1

  1. import java.util.Scanner;
  2. public class Chapter11_ProjectPinochle {
  3. public static void main(String[]args) {
  4. Scanner sc = new Scanner(System.in);
  5. String a, b = "";
  6. System.out.println("Type in your pinochle deals: ");
  7. a = sc.nextLine();
  8. sc.close();
  9. String[] temp = a.split(",");
  10. for (int i = 0; i < temp.length; i++) {
  11. for (int j = 0; j < temp[i].length(); j++) {
  12. b += temp[i].charAt(j);
  13. }
  14. }
  15. char[] deals = new char[b.length()];
  16. for (int i = 0; i < b.length(); i++) {
  17. deals[i] = b.charAt(i);
  18. }
  19. }
  20. }
英文:

Try this code

  1. import java.util.Scanner;
  2. public class Chapter11_ProjectPinochle {
  3. public static void main(String[]args) {
  4. Scanner sc = new Scanner(System.in);
  5. String a,b=&quot;&quot;;
  6. System.out.println(&quot;Type in your pinochle deals: &quot;);
  7. a = sc.nextLine();
  8. sc.close();
  9. String[] temp = a.split(&quot;,&quot;);
  10. for (int i=0; i&lt;temp.length; i++){
  11. for (int j=0; j&lt;temp[i].length(); j++){
  12. b+=temp[i].charAt(j);
  13. }
  14. }
  15. char[] deals=new char[b.length()];
  16. for (int i=0; i&lt;b.length(); i++){
  17. deals[i]=b.charAt(i);
  18. }
  19. }
  20. }

答案3

得分: 0

这里是您要的内容:

  1. String a = "ATKQQJ,AKQQ,KQQJN";
  2. // 将字符串按逗号分隔成部分
  3. String[] parts = a.split(",");
  4. // 将这些部分合并为一个单独的字符串
  5. String whole = String.join("", parts);
  6. // 最后,将其分割为单个字母
  7. String[] letters = whole.split("");

也可以通过从 a 中删除逗号来生成 whole

英文:

Here you go:

  1. String a = &quot;ATKQQJ,AKQQ,KQQJN,A&quot;;
  2. // Split the string into comma-separated parts
  3. String[] parts = a.split(&quot;,&quot;);
  4. //Join those parts into a single string
  5. String whole = String.join(&quot;&quot;,parts);
  6. //Finally, split it up into individual letters
  7. String[] letters = whole.split(&quot;&quot;);

Could have also generated whole by removing the commas from a.

huangapple
  • 本文由 发表于 2020年3月17日 03:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/60712354.html
匿名

发表评论

匿名网友

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

确定