简体   繁体   中英

How to merge two “strings” with unique values to new String in java? Using converting to arrays or lists

can someone please help me to solve this? Thank you

String a = 1||2||3
String b = 2||4||5

String c = 1||2||3||4||5|| //Must be uniqe

Using converting to arrays or lists? I do not know what your mean. but I suggest

String a = "1||2||3";
String b = "2||4||5";
String[] arrayA = a.split("||");
String[] arrayB = b.split("||");
List<String> list = Arrays.asList(arrayA);
list.addAll(Arrays.asList(arrayB));            
System.console().printf(list.stream().distinct().collect(Collectors.joining("||")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM