Split Character String at Whitespace in R Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we are going to discuss how to split character strings at white space in R programming language. Method 1: Using strsplit() function strsplit() function is used to split the string based on some condition. Syntax: strsplit(input_string, " +") where input_string is the string" +" represents to be split at white space Example: R program to split a given string R # consider a string with white spaces string1="Hello Geeks we are in Java Python R and CPP" # split the string by using strsplit() function print(strsplit(string1, " +")) Output: [[1]] [1] "Hello" "Geeks" "we" "are" "in" "Java" "Python" "R" [9] "and" "CPP" It is stored in index 1 in a list so we can modify our code a little by using the index 1 position. Example: R program to split a given string R # consider a string with white spaces string1="Hello Geeks we are in Java Python R and CPP" # split the string by using strsplit() function print(strsplit(string1, " +")[[1]]) Output: [1] "Hello" "Geeks" "we" "are" "in" "Java" "Python" "R" [9] "and" "CPP" Method 2: Using scan() function This function is also used to split the string by scanning the elements. Syntax: scan(text = input_string, what = "") Where, the text parameter is used to store input stringwhat is a parameter that can take white space which specifies the string to be split at whitespace It will also display how many times is scanned (It will return the number of split words). Example: R program to split a string at white space using scan() function R # consider a string with white spaces string1="Hello Geeks we are in Java Python R and CPP" # split the string by using scan() function print(scan(text = string1, what = "")) Output: Read 10 items [1] "Hello" "Geeks" "we" "are" "in" "Java" "Python" "R" [9] "and" "CPP" Example: R program to split a string at white space using scan() function R # consider a string with white spaces string1="There are big branches in India" # split the string by using scan() function print(scan(text = string1, what = "")) Output: Read 6 items [1] "There" "are" "big" "branches" "in" "India" Comment More infoAdvertise with us Next Article Remove All Special Characters from String in R S sravankumar_171fa07058 Follow Improve Article Tags : R Language R Programs R-strings R String-Programs Similar Reads Remove All White Space from Character String in R In this article, we are going to see how to remove all the white space from character string in R Programming Language. We can do it in these ways: Using gsub() function.Using str_replace_all() function.Method 1: Using gsub() Function gsub() function is used to remove the space by removing the space 2 min read Remove All Special Characters from String in R In this article, we are going to remove all special characters from strings in R Programming language. For this, we will use the str_replace_all() method to remove non-alphanumeric and punctuations which is available in stringr package. Installation To install this library type the below command in 1 min read Replace Specific Characters in String in R In this article, we will discuss how to replace specific characters in a string in R Programming Language. Method 1: Using gsub() function We can replace all occurrences of a particular character using gsub() function. Syntax: gsub(character,new_character, string) Parameters: string is the input st 3 min read Extract Numbers from Character String Vector in R In this article, we are going to see how to extract Numbers from Character String Vector in R Programming Language. There are different approaches to extract numbers from character string vectors using some in-built functions. It can be done in the following ways:Using the gsub() functionUsing the g 2 min read Extract Numbers from Character String Vector in R In this article, we are going to see how to extract Numbers from Character String Vector in R Programming Language. There are different approaches to extract numbers from character string vectors using some in-built functions. It can be done in the following ways:Using the gsub() functionUsing the g 2 min read Remove All Whitespace in Each DataFrame Column in R In this article, we will learn how to remove all whitespace in each dataframe column in R programming language. Sample dataframe in use: c1 c2 1 geeks for geeks 2 cs f 3 r -lang gMethod 1: Using gsub() In this approach, we have used apply() function to apply a function to each row in a data frame. T 4 min read Like