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

```
To get the number of R's before the first u, you need to make an intermediate step. (You probably don't need to. I'm sure akrun knows some incredibly cool regular expression to get the job done, but it won't be as easy to understand as this).

```
rquote <- "R's internals are irrefutably intriguing"
before_u <- gsub("u[[:print:]]+$", "", rquote)
length(stringr::str_extract_all(before_u, "(R|r)")[[1]])

```