mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 00:58:02 +00:00
Add Splitting On Whitespace as a clojure til.
This commit is contained in:
@@ -7,6 +7,10 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
|
### clojure
|
||||||
|
|
||||||
|
- [Splitting On Whitespace](clojure/splitting-on-whitespace.md)
|
||||||
|
|
||||||
### git
|
### git
|
||||||
|
|
||||||
- [Checkout Previous Branch](git/checkout-previous-branch.md)
|
- [Checkout Previous Branch](git/checkout-previous-branch.md)
|
||||||
|
|||||||
34
clojure/splitting-on-whitespace.md
Normal file
34
clojure/splitting-on-whitespace.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Splitting On Whitespace
|
||||||
|
|
||||||
|
If you have a string with spaces and you want to split the string into a
|
||||||
|
vector of strings (delimited by the spaces), then you can do something like
|
||||||
|
this:
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
(clojure.string/split "split me up" #" ")
|
||||||
|
; ["split" "me" "up"]
|
||||||
|
```
|
||||||
|
|
||||||
|
However, if you have extra spaces in your string, the output may not be quite
|
||||||
|
what you want:
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
(clojure.string/split "double spacing wtf?" #" ")
|
||||||
|
; ["double" "" "spacing" "" "wtf?"]
|
||||||
|
```
|
||||||
|
|
||||||
|
A quick fix might look like this:
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
(clojure.string/split "double spacing wtf?" #"[ ]+")
|
||||||
|
; ["double" "spacing" "wtf?"]
|
||||||
|
```
|
||||||
|
|
||||||
|
That's nice, but it is going to fall over as soon as we run into input with
|
||||||
|
tabs and new lines. Assuming we want to split on all whitespace, we should
|
||||||
|
tell our regular expression to do just that:
|
||||||
|
|
||||||
|
```clojure
|
||||||
|
(clojure.string/split "we\thave new\nlines and tabs\n" #"[\s]+")
|
||||||
|
; ["we" "have" "new" "lines" "and" "tabs"]
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user