Question: I have this API call like so:

```
var items = []

    override func viewDidLoad() {

        let url = NSURL(string: "API.php")

        let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
            println(NSString(data: data, encoding: NSUTF8StringEncoding))
        }

        task.resume()

        super.viewDidLoad()
    }

```

What I am trying to do is add each item to the array items, I have tried the following:

`items += [NSString(data: data, encoding: NSUTF8StringEncoding))` but that didnt work, does anyone know how to do this ? I am new to swift as of this morning.

When i say it didn't work, it just took my ajax data and has it as one string.

Answer: 
You could use substrings.

```
## find position of first 'u'
u1 <- regexpr("u", rquote, fixed = TRUE)
## get count of all 'r' or 'R' before 'u1'
lengths(gregexpr("r", substr(rquote, 1, u1), ignore.case = TRUE))
# [1] 5

```

This follows what you ask for in the title of the post. If you want the count of all the "r", case insensitive, then simplify the above to

```
lengths(gregexpr("r", rquote, ignore.case = TRUE))
# [1] 6

```

Then there's always `stringi`

```
library(stringi)
## count before first 'u'
stri_count_regex(stri_sub(rquote, 1, stri_locate_first_regex(rquote, "u")[,1]), "r|R")
# [1] 5
## count all R or r
stri_count_regex(rquote, "r|R")
# [1] 6

```
You may try this,

```
> length(str_extract_all(rquote, '[Rr]')[[1]])
[1] 6

```

To get the count of all `r`'s before the first `u`

```
> length(str_extract_all(rquote, perl('u.*(*SKIP)(*F)|[Rr]'))[[1]])
[1] 5

```