从Java中获取字符串的子字符串

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

Getting substrings from a string in Java

问题

我有一个像这样的字符串:

String ColorCodes = "colorcodes:#FFOOFF,#OOACOF,#EEAAAA";

我想将上述字符串中的三个颜色代码分别提取到三个不同的字符串中,就像这样:

String ColorOne = "#FFOOFF";
String ColorTwo = "#OOACOF";
String ColorThree = "#EEAAAA";

我该如何实现这个?

英文:

I have a string like this:

String ColorCodes = "colorcodes:#FFOOFF,#OOACOF,#EEAAAA"

I want to get the three color codes in the above string into three different strings, like this:

String ColorOne = "#FFOOFF"
String ColorTwo = "#OOACOF"
String ColorThree = "#EEAAAA"

How can I do this?

答案1

得分: 1

可以使用 substringsplit

String[] colors = colorCodes.substring(colorCodes.indexOf(":") + 1).split(",");
        
System.out.println(Arrays.toString(colors));

输出

[#FFOOFF, #OOACOF, #EEAAAA]
英文:

You can use substringand split:

    String[] colors = colorCodes.substring(colorCodes.indexOf(":") + 1).split(",");
            
    System.out.println(Arrays.toString(colors));

Output

[#FFOOFF, #OOACOF, #EEAAAA]

huangapple
  • 本文由 发表于 2020年7月27日 02:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63104199.html
匿名

发表评论

匿名网友

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

确定