algorithmd# Splitting a string in d programming language via whitespace where multiple whitespace can appear consecutively but should be treated as one

I want to split a string in D programming language, so that elements that are empty strings, are not counted.

Example:

Input : `This is a string`

[Note that between `is`

and `a`

, there are 3 blank spaces]

Output: `[This, is, a, string]`

**Problem**

If I use the `std.array.split`

[linked here] function with `" "`

(blank space) as delimiter, then I get: `["This", "is", " ", "a", "string"]`

. See the blank element between `"is"`

and `"a"`

.

**My Current solution**

```
output = input.split(" ").filter!(l => !l.strip().empty).array;
```

Note, that this would also be possible if multiple consecutive blank spaces were treated as one.

**My question**

Does the split function (or an alternative) has a built in method to either:

- automatically treat multiple consecutive occurrence of the delimiter as one,
- automatically reject elements that are whitespace only

Either of these two will be sufficient for this particular example (I can't think of a counterexample)

I looked at Programming in D – Tutorial and Reference by Ali Çehreli [here], but i can't seem to find this functionality. Does that mean in D you are supposed to use the filter and Lambda?

Thank you for your help.

Solution

`split`

without any parameters does exactly what you are requesting:

```
When no delimiter is provided, strings are split into an array of words,
using whitespace as delimiter. Runs of whitespace are merged together
(no empty words are produced).
```

(this is a quote from the documentation which you linked to)

- Uniformly randomly generate a vector of k unsigned ints that sums to N
- Mapping elementwise Tuple Using Template Function
- Find the longest prefix for a table of tokenized strings
- Kalman Filter Prediction Implementation
- My ALV Tree balance factors calculate incorrectly
- Infix to postfix left one parentheses at the end when expression is fully enclosed
- C++ quick sort algorithm
- Identify connected subnetworks (R-igraph)
- Merge Sort Function not working when size of vector is not 2^n
- Given an array a of n integers, and q queries, for each value from index l to index r, add x to the value
- Count mountain arrays
- Get the number of all possible combinations that give a certain product
- getting TLE in leetcode question 212- word search 2 using backtrcaking even after pruning, how do i optimize it more
- Maximizing the sum of Adjacent sum in an array
- Weighted random selection from array
- If f(n) = O(g(n)), is log(f(n)) = O(log(g(n))?
- Why is KNN slow with custom metric?
- Based on a condition, how to remove an item from the processed array and move it to another array?
- The simplest algorithm for poker hand evaluation
- Find longest repetitive sequence in a string
- Check if all elements in a list are identical
- Minimize sum of products of adjacent elements of an array
- Creating dijkstras algorithm and having issues in C language
- Sliding subarray beauty working on IDE but not on leetcode
- How to insert a new element into an array in C?
- Permutations without recursive function call
- Algorithms and Data structure
- python - prefix sum algorithm
- Converting a Person's Height from feet and inches to just inches C#
- Reversible "hash" function from 64-bit integer to 64-bit integer