← Back to R Course | Chapter 6: Strings | Lesson 6 of 7

strsplit

In this page:

  1. strsplit

strsplit

strsplit(x, split) splits a string into pieces wherever the separator pattern occurs, returning a list (since each input string could split into a different number of pieces). Accessing the first result requires indexing into the list, like strsplit(x, ",")[[1]]. This is the standard way to parse delimited text like CSV rows.

Note: strsplit() always returns a list, even for a single string input -- use [[1]] to get the character vector of pieces out.

Example: strsplit

markup
csv_line <- "apple,banana,cherry"

parts <- strsplit(csv_line, ",")[[1]]
print(parts)
cat("Second item:", parts[2], "\n")
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.