用两个元素分割字符串?

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

Splitting a String by two things?

问题

import java.util.Scanner;

public class Chapter11_ProjectPinochle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a;
        System.out.println("Type in your pinochle deals: ");
        a = sc.nextLine();
        sc.close();
        String[] deals = a.split(",");
        String concatenatedDeals = String.join("", deals);
        String[] finalDeals = concatenatedDeals.split("");
    }
}
import java.util.Scanner;

public class Chapter11_ProjectPinochle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a;
        System.out.println("Type in your pinochle deals: ");
        a = sc.nextLine();
        sc.close();
        String[] deals = a.split(",");
        String concatenatedDeals = String.join("", deals);
        String[] finalDeals = concatenatedDeals.split("");

        // At this point, finalDeals will contain the desired output.
        // You can print it to verify.
        System.out.print("[");
        for (int i = 0; i < finalDeals.length; i++) {
            System.out.print("\"" + finalDeals[i] + "\"");
            if (i < finalDeals.length - 1) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }
}
英文:

I have the following code:

import java.util.Scanner;
public class Chapter11_ProjectPinochle {
public static void main(String[]args) {
	Scanner sc=new Scanner(System.in);
	String a;
	System.out.println(&quot;Type in your pinochle deals: &quot;);
	a=sc.nextLine();
	sc.close();
	String[] deals=a.split(&quot;&quot;);	
}
}

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

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

答案2

得分: 1

import java.util.Scanner;

public class Chapter11_ProjectPinochle {
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        String a, b = "";
        System.out.println("Type in your pinochle deals: ");
        a = sc.nextLine();
        sc.close();
        String[] temp = a.split(",");
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp[i].length(); j++) {
                b += temp[i].charAt(j);
            }
        }
        char[] deals = new char[b.length()];
        for (int i = 0; i < b.length(); i++) {
            deals[i] = b.charAt(i);
        }
    }
}
英文:

Try this code

import java.util.Scanner;

public class Chapter11_ProjectPinochle {
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        String a,b=&quot;&quot;;
        System.out.println(&quot;Type in your pinochle deals: &quot;);
        a = sc.nextLine();
        sc.close();
        String[] temp = a.split(&quot;,&quot;);
        for (int i=0; i&lt;temp.length; i++){
            for (int j=0; j&lt;temp[i].length(); j++){
                b+=temp[i].charAt(j);
            }
        }
        char[] deals=new char[b.length()];
        for (int i=0; i&lt;b.length(); i++){
            deals[i]=b.charAt(i);
        }

    }

}

答案3

得分: 0

这里是您要的内容:

String a = "ATKQQJ,AKQQ,KQQJN";

// 将字符串按逗号分隔成部分
String[] parts = a.split(",");

// 将这些部分合并为一个单独的字符串
String whole = String.join("", parts);

// 最后,将其分割为单个字母
String[] letters = whole.split("");

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

英文:

Here you go:

    String a = &quot;ATKQQJ,AKQQ,KQQJN,A&quot;;

    // Split the string into comma-separated parts
    String[] parts = a.split(&quot;,&quot;);

    //Join those parts into a single string
    String whole = String.join(&quot;&quot;,parts);

    //Finally, split it up into individual letters
    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:

确定