Split String by Components and Keep Components in Place

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



Leave a reply



Submit