Remove and get text, from comma separated string if it has # as prefix in java?

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

Remove and get text, from comma separated string if it has # as prefix in java?

问题

You can achieve this in Java by splitting the input string and then checking each element to see if it contains the special character '#'. Here's the code to do that:

String str = "1,2,3,11,#5,#7,9";
String[] elements = str.split(",");
List<String> resultList = new ArrayList<>();
List<String> specialCharactersList = new ArrayList<>();

for (String element : elements) {
    if (element.contains("#")) {
        specialCharactersList.add(element.replace("#", ""));
    } else {
        resultList.add(element);
    }
}

String result = String.join(",", resultList);
// Now, resultList contains "1,2,3,11,9" and specialCharactersList contains "5" and "7"

Please note that you should import the necessary classes (java.util.List, java.util.ArrayList) at the beginning of your Java file.

英文:

I have a string in java as

Input

String str = &quot;1,2,3,11,#5,#7,9&quot;;

Output Desired

String result = &quot;1,2,3,11,9&quot;;
// And 5,7 with special character # in separate Array-list
//List&lt;String&gt; list = [&quot;5&quot;,&quot;7&quot;];

Note: This special character # is dynamic, it may or may not be present in string.

I know how to remove # using str.replaceAll("#", "");, but how to get 5 and 7 in a separate list.

答案1

得分: 1

以下是代码的翻译部分:

String str = "1,2,3,11,#5,#7,9";
List<String> parts = Arrays.asList(str.split(","));
List<Integer> normalNumbers = parts.stream().filter(i -> !i.startsWith("#")).map(Integer::parseInt).collect(
        Collectors.toList());
List<Integer> specialNumbers = parts.stream().filter(i -> i.startsWith("#")).map(i -> Integer.valueOf(i.substring(1))).collect(Collectors.toList());
System.out.println(normalNumbers);
System.out.println(specialNumbers);
英文:
    String str = &quot;1,2,3,11,#5,#7,9&quot;;
    List&lt;String&gt; parts = Arrays.asList(str.split(&quot;,&quot;));
    List&lt;Integer&gt; normalNumbers = parts.stream().filter(i -&gt; !i.startsWith(&quot;#&quot;)).map(Integer::parseInt).collect(
            Collectors.toList());
    List&lt;Integer&gt; specialNumbers = parts.stream().filter(i -&gt; i.startsWith(&quot;#&quot;)).map(i -&gt; Integer.valueOf(i.substring(1))).collect(Collectors.toList());
    System.out.println(normalNumbers);
    System.out.println(specialNumbers);

答案2

得分: 0

在Java中,String类有一个名为split()的方法。您可以轻松使用这个方法来获取所有带有和不带有前缀#的整数。

String str = "1,2,3,11,#5,#7,9";

List<Integer> res = Arrays.asList(str.split(",")).stream().map(ch -> {
    if (!ch.startsWith("#")) {
        return Integer.parseInt(ch);
    } else {
        return null;
    }
}).filter(Objects::nonNull).collect(Collectors.toList());

System.out.println(res);

(请注意,由于您要求只返回翻译好的部分,我只提供了代码的翻译部分。)

英文:

String in Java has a method called split(). You could easily use this to get all Integers with & without the prefix #

        String str = &quot;1,2,3,11,#5,#7,9&quot;;
        
        List&lt;Integer&gt; res = Arrays.asList(str.split(&quot;,&quot;)).stream().map(ch-&gt;{
            if(!ch.startsWith(&quot;#&quot;)){
                return Integer.parseInt(ch);
            }else {
                return null;
            }
        }).filter(Objects::nonNull).collect(Collectors.toList());

        System.out.println(res);

答案3

得分: 0

String str = "1,2,3,11,#5,#7,9";
List<String> s = Arrays.asList(str.split(",")).stream().filter(a -> a.startsWith("#")).map(a -> a.replace("#", "")).collect(Collectors.toList());
System.out.println(s);  //Output [5, 7]
str = Arrays.asList(str.split(",")).stream().filter(a -> !a.startsWith("#")).collect(Collectors.joining(","));
System.out.println(str); //Output 1,2,3,11,9
英文:
String str = &quot;1,2,3,11,#5,#7,9&quot;;
List&lt;String&gt; s = Arrays.asList(str.split(&quot;,&quot;)).stream().filter(a-&gt;a.startsWith(&quot;#&quot;)).map(a-&gt;a.replace(&quot;#&quot;, &quot;&quot;)).collect(Collectors.toList());
System.out.println(s);  //Output [5, 7]
str = Arrays.asList(str.split(&quot;,&quot;)).stream().filter(a-&gt;!a.startsWith(&quot;#&quot;)).collect(Collectors.joining(&quot;,&quot;));
System.out.println(str); //Output 1,2,3,11,9

答案4

得分: 0

  1. 你可以尝试使用String.split()方法,
String[] tokens = str.split(",");
  1. 创建两个新的String数组列表,
ArrayList<String> num = new ArrayList<String>();
ArrayList<String> result = new ArrayList<String>();
  1. 然后遍历这些分词,检查每个分词,然后进行操作,
for(int i = 0; i < tokens.length; i++)
{
    if(tokens[i].charAt(0) == '#')
        num.add(String.valueOf(tokens[i].charAt(1)));
    else
        result.add(tokens[i]);
}
英文:

1. You could try String.split(),

String[] tokens = str.split(&quot;,&quot;); 

2. Create 2 new String Array-List,

ArrayList&lt;String&gt; num = new ArrayList&lt;String&gt;();
ArrayList&lt;String&gt; result = new ArrayList&lt;String&gt;();

3. Then loop thru the tokens, checking each token, then push,

for(int i = 0; i &lt; tokens.length; i++)
{
    if(tokens[i].charAt(0) == &#39;#&#39;)
        num.add(String.valueOf(tokens[i].charAt(1)));
    else
        result.add(tokens[i]);
}

huangapple
  • 本文由 发表于 2020年8月3日 14:43:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224854.html
匿名

发表评论

匿名网友

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

确定