英文:
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 = "1,2,3,11,#5,#7,9";
Output Desired
String result = "1,2,3,11,9";
// And 5,7 with special character # in separate Array-list
//List<String> list = ["5","7"];
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 = "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);
答案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 = "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);
答案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 = "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
答案4
得分: 0
- 你可以尝试使用String.split()方法,
String[] tokens = str.split(",");
- 创建两个新的String数组列表,
ArrayList<String> num = new ArrayList<String>();
ArrayList<String> result = new ArrayList<String>();
- 然后遍历这些分词,检查每个分词,然后进行操作,
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(",");
2. Create 2 new String Array-List,
ArrayList<String> num = new ArrayList<String>();
ArrayList<String> result = new ArrayList<String>();
3. Then loop thru the tokens, checking each token, then push,
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]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论