Split string by components and keep components in place
For that you need to loop through the String
and check its each characters
that is it tokens or not. You can make extension
of String
for that like this.
extension String {
func stringTokens(splitMarks: Set<String>) -> [String] {
var string = ""
var desiredOutput = [String]()
for ch in self.characters {
if splitMarks.contains(String(ch)) {
if !string.isEmpty {
desiredOutput.append(string)
}
desiredOutput.append(String(ch))
string = ""
}
else {
string += String(ch)
}
}
if !string.isEmpty {
desiredOutput.append(string)
}
return desiredOutput
}
}
Now you can call this function like this way.
let input = "foo&bar|hello"
print(input.stringTokens(splitMarks: ["&", "|"]))
Output
["foo", "&", "bar", "|", "hello"]
Split String into Array keeping delimiter/separator in Swift
Suppose you are splitting the string by a separator called separator
, you can do the following:
let result = yourString.components(separatedBy: separator) // first split
.flatMap { [$0, separator] } // add the separator after each split
.dropLast() // remove the last separator added
.filter { $0 != "" } // remove empty strings
For example:
let result = " Hello World ".components(separatedBy: " ").flatMap { [$0, " "] }.dropLast().filter { $0 != "" }
print(result) // [" ", "Hello", " ", "World", " "]
In Python, how do I split a string and keep the separators?
>>> re.split('(\W)', 'foo/bar spam\neggs')
['foo', '/', 'bar', ' ', 'spam', '\n', 'eggs']
Splitting a path when the path delimiter is used in a component name
Using a match, you might use:
(?:[^\n\/\\]+|\\[\\\/]?)+
Explanation
(?:
No capture group[^\n\/\\]+
Match any char except a newline/
or\
|
Or\\[\\\/]?
Match\
and optional\
or/
)+
Close non capture group and repeat 1+ times
Regex demo
Then in the matches, you can replace \/
with /
const regex = /(?:[^\n\/\\]+|\\[\\\/]?)+/g;
[
String.raw `aaaa/bbb\/ccc`,
String.raw `this/is\/a/\/str\\/ing`,
String.raw `aaaa/bbb\\/ccc/ddd\/eee/fff/ggg`,
String.raw `this/is\/a/dumb/str\\/ing`,
String.raw `aaaa/\\bbb`
].forEach(s =>
console.log(Array.from(s.matchAll(regex), m => m[0].replace("\\/", "/")))
);
Java split string and store that two parts of string into arraylist
ArrayList<String> list= Arrays.asList(Your_String.split("delimiter"));
Split string into two parts only
You could use a,b = split(' ', 1)
.
The second argument 1
is the maximum number of splits that would be done.
s = 'abcd efgh hijk'
a,b = s.split(' ', 1)
print(a) #abcd
print(b) #efgh hijk
For more information on the string split function, see str.split
in the manual.
Split a string with delimiters but keep the delimiters in the result in C#
If you want the delimiter to be its "own split", you can use Regex.Split e.g.:
string input = "plum-pear";
string pattern = "(-)";
string[] substrings = Regex.Split(input, pattern); // Split on hyphens
foreach (string match in substrings)
{
Console.WriteLine("'{0}'", match);
}
// The method writes the following to the console:
// 'plum'
// '-'
// 'pear'
So if you are looking for splitting a mathematical formula, you can use the following Regex
@"([*()\^\/]|(?<!E)[\+\-])"
This will ensure you can also use constants like 1E-02 and avoid having them split into 1E, - and 02
So:
Regex.Split("10E-02*x+sin(x)^2", @"([*()\^\/]|(?<!E)[\+\-])")
Yields:
10E-02
*
x
+
sin
(
x
)
^
2
Related Topics
Get the String Up to a Specific Character
How to Declare Exponent/Power Operator with New Precedencegroup in Swift 3
Limit Textfield to X Amount of Characters Using Swiftui
Animate an Skspritenode with Textures That Have a Size Different from the Original
MAC App to Switch Between /Etc/Hosts Files, How to Allow Access
Swift: Get 30 Days Before 'Specific Date'
How to Catch Error When Setting Launchpath in Nstask
How to Extend Float3 or Any Other Built-In Type to Conform to the Codable Protocol
What Does T Equals T Mean in Generics
Moving Skspritenode to Location of the Touch
Dismiss View from View Model [Modal Page]
Tapping an Mkmapview in Swiftui
Play Segment of Avaudiopcmbuffer
Saving Highscores with Nsuserdefaults
Instance Member Cannot Be Used on Type | Closures