Multisplit: Difference between revisions

1,392 bytes added ,  13 years ago
+Java, F# incorrect
(+Java, F# incorrect)
Line 43:
 
=={{header|F_Sharp|F#}}==
{{incorrect|F Sharp|<nowiki>In the second example, "!=" should not be matched since all "=" chars should be removed by the first separator listed.</nowiki>}}
If we ignore the "Extra Credit" requirements and skip 'ordered separators' condition (i.e. solving absolute different task), this is exactly what one of the overloads of .NET's <code>String.Split</code> method does. Using F# Interactive:
<lang fsharp>> "a!===b=!=c".Split([|"=="; "!="; "="|], System.StringSplitOptions.None);;
Line 93 ⟶ 94:
│0 1│3 2│ │
└───┴───┴─┘</lang>
=={{header|Java}}==
{{works with|Java|1.5+}}
<lang java5>import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
 
public class Multisplit {
public static List<String> multisplit(String str, String... seps){
List<String> ans = new ArrayList<String>();
ans.add(str);
if(seps.length == 0){
return ans;
}
for(String sep : seps){
//temporary list holding substrings with new separators removed
List<String> sepsRemoved = new ArrayList<String>();
for(String substr : ans){
//quote in case they use regex chars as a separator
sepsRemoved.addAll(Arrays.asList(substr.split(Pattern.quote(sep))));
}
ans.clear();
ans.addAll(sepsRemoved);
//condense places where multiple separators were matched consecutively
ans.remove("");
}
return ans;
}
public static void main(String[] args){
System.out.println(multisplit("a!===b=!=c", "==", "!=", "="));
System.out.println(multisplit("a!===b=!=c", "=", "!=", "=="));
System.out.println(multisplit("a!===b=!=c"));
}
}</lang>
Output (the brackets are added as part of the toString for Lists -- they are not part of the results in the list):
<pre>[a!, b, c]
[a!, b, !, c]
[a!===b=!=c]</pre>
=={{header|PicoLisp}}==
<lang PicoLisp>(de multisplit (Str Sep)
Anonymous user