![](/img/trans.png)
[英]How to filter unique values of array object when in comma separated string
[英]Java Filter string values separated by comma
下面是用逗號分隔的值字符串。 需要過濾每個值和 append 它們具有特定值。
字符串值 - Hello, MyName, Is, XYZ
以上是我通過 API 發送的字符串
我想要的是 - 如何將(_true)
這個字符串值添加到上面String Values上的每個元素。 這也是我在某些特定條件下檢查的幾個字符串值
所以它應該像下面發送到前端的 API 調用一樣。
Hello(_true), MyName, Is, XYZ(_true)
public static String StringAppenderWithLogic(String given, String del) {
return String.join(del, Arrays.stream(given.split(del)).
map(p -> {
//put any logic specific to your requirement
if (p.trim().startsWith("H") || p.trim().startsWith("X")) {
p = p + "(_true)";
}
return p;
}).collect(Collectors.toList()));
}
你可以試試這個
import java.util.*;
import java.util.stream.Collectors;
public class Main
{
public static void main(String[] args) {
String apiOutput = "Hello, MyName, Is, XYZ";
List<String> s = List.of(apiOutput.split(", ")).stream()
.map(Main::stringController)
.collect(Collectors.toList());
String result = String.join(", ", s);
System.out.println(result);
}
private static String stringController(String in){
if(hasMatchCondition(in)){
return in + "(_true)";
}
return in;
}
private static boolean hasMatchedCondition(String in){
//place your logic here
//[START] exemple
if(in.length() == 5 || in.length() == 3)
return true;
return false;
//[END] exemple
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.