如何在Java中拆分括号和逗号

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

How to split parenthesis and , in Java

问题

我想要分割这个字符串 (1,0),并且得到结果 1 和 0。我尝试了这段代码:

String str = "(1,0)";
String parts[] = str.split("[\\(,\\)]");
System.out.println(parts[0]);
System.out.println(parts[1]);

但是我得到了这个结果:

(1

英文:

I want to split this string (1,0) and get result 1 and 0 i have tried this code:

String str ="(1,0)";
String parts[]= str.split("(,)");
System.out.println(parts[0]);
System.out.println(parts[1]);

But i got this :

(1

答案1

得分: 1

以下是您可以使用正则表达式工具高效地分离出所有数字并将它们放入ArrayList以便轻松使用的方法。它不使用.split()方法,但效率很高。

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main {
    public static void main(String[] args) {
        String str = "(1,0)";
        
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher(str);
        ArrayList<Integer> vals = new ArrayList<>();
        
        while(m.find())
            vals.add(Integer.parseInt(m.group()));
        
        System.out.println(vals);
    }
}
英文:

Here's an efficient way you can isolate all your digits using the Regex Tools and put them into an ArrayList for easy usage. It doesn't use the .split() method, but it is efficient.

import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public static void main(String[] args) {
    String str = &quot;(1,0)&quot;;
    
    Pattern p = Pattern.compile(&quot;\\d+&quot;);
    Matcher m = p.matcher(str);
    ArrayList&lt;Integer&gt; vals = new ArrayList&lt;&gt;();
    
    while(m.find())
      vals.add(Integer.parseInt(m.group()));
    
    System.out.println(vals);
}

答案2

得分: 0

如果您在(上进行拆分,返回数组中的第一个值将是&quot;&quot;

我建议直接使用正则表达式,例如:

String input = "(1,0)";
Matcher m = Pattern.compile("\\(([^,\\)]+),([^,\\)]+)\\)").matcher(input);
if (!m.matches())
    throw new IllegalArgumentException("Invalid input: " + input);
System.out.println(m.group(1));
System.out.println(m.group(2));

当然,如果您坚持使用split(),可以这样做:

String input = "(1,0)";
String[] parts = input.split("[(),]");
if (parts.length != 3 || !parts[0].isEmpty())
    throw new IllegalArgumentException("Invalid input: " + input);
System.out.println(parts[1]);
System.out.println(parts[2]);
英文:

If you split on (, the first value in the returned array will be &quot;&quot;.

I'd recommend using regex directly, e.g.

String input = &quot;(1,0)&quot;;
Matcher m = Pattern.compile(&quot;\\(([^,\\)]+),([^,\\)]+)\\)&quot;).matcher(input);
if (! m.matches())
    throw new IllegalArgumentException(&quot;Invalid input: &quot; + input);
System.out.println(m.group(1));
System.out.println(m.group(2));

Of course, if you insist on using split(), it can be done like this:

String input = &quot;(1,0)&quot;;
String[] parts = input.split(&quot;[(,)]&quot;);
if (parts.length != 3 || ! parts[0].isEmpty())
    throw new IllegalArgumentException(&quot;Invalid input: &quot; + input);
System.out.println(parts[1]);
System.out.println(parts[2]);

答案3

得分: 0

以下是翻译好的内容:

一个简单的解决方案如下

    public class Main {
        public static void main(String[] args) {
            String str = "(1,0)";
            String parts[] = str.replace("(", "").replace(")", "").split(",");
            System.out.println(parts[0]);
            System.out.println(parts[1]);
        }
    }

**输出**

    1
    0
英文:

A simple solution would be as follows:

public class Main {
	public static void main(String[] args) {
		String str = &quot;(1,0)&quot;;
		String parts[] = str.replace(&quot;(&quot;, &quot;&quot;).replace(&quot;)&quot;, &quot;&quot;).split(&quot;,&quot;);
		System.out.println(parts[0]);
		System.out.println(parts[1]);
	}
}

Output:

1
0

答案4

得分: 0

如果您知道如何使用正则表达式,可以尝试使用它。(就个人而言,我更喜欢在这里使用字符串操作,因为它真的更简单。)如果不会,可以学习如何使用,或者像这样做:

String input = "(64,128)";
String[] numbers = input.substring(1, input.length() - 1).split(",");
英文:

If you know how to use regex, go for that. (personally I prefer to use string manipulation here because it's really easier) If not, learn how to use it or do something like this:

String input = &quot;(64,128)&quot;;
String[] numbers = input.substring(1, input.length() - 1).split(&quot;,&quot;);

答案5

得分: 0

试一试这个,假设你的格式是一致的。
		String str = "(1,0)";
		String[] tokens = str.substring(1, str.length() - 1).split(",");
		System.out.println(Arrays.toString(tokens));
打印输出
[1, 0]
或者如果分开打印
1
0
英文:

Try this, assuming your format is consistent.

		String str = &quot;(1,0)&quot;;
		String[] tokens = str.substring(1,str.length()-1).split(&quot;,&quot;);
		System.out.println(Arrays.toString(tokens));

Prints

[1, 0]

or if printed separately

1
0

huangapple
  • 本文由 发表于 2020年4月9日 05:13:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61110130.html
匿名

发表评论

匿名网友

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

确定