Remove Newline from Character String in R Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to remove the new line from a character string in R Programming Language. Example: Input: String with newline: "Hello\nGeeks\rFor\r\n Geeks" Output: String after removing new line: "HelloGeeksFor Geeks"Method 1: Using gsub() function gsub() function in R Language is used to replace all the matches of a pattern from a string. If the pattern is not found the string will be returned as it is. Syntax:gsub(pattern, replacement, string, ignore.case=TRUE/FALSE) Parameters:pattern: string to be matchedreplacement: string for replacementstring: String or String vectorignore.case: Boolean value for case-sensitive replacement Example: R program to remove the newline from character string using gsub() function. Syntax: gsub("[\r\n]", "", string) where [\r\n] is a first parameter which is a pattern to remove new lines"" is the second parameter that replaces when new line occurs as emptystring is the input string of characters. Code: R # consider a string string = "Hello\nGeeks\nFor\nGeeks\n" # display string print("Original string: ") cat(string) # string after removing new lines print("string after removing new lines: ") cat(gsub("[\r\n]", "", string)) Output: [1] "Original string: " Hello Geeks For Geeks [1] "string after removing new lines: " HelloGeeksForGeeksMethod 2 : Using str_replace_all() function This function is available in stringr package which is also used to remove the new line from a character string Syntax: str_replace_all(string, "[\r\n]" , "") where string is the first parameter that takes string as input.[\r\n] is a second parameter which is a pattern to remove new lines"" is the third parameter that replaces when new line occurs as empty Example: R program to remove newline from string using stringr package R library(stringr) # consider a string string="Hello\nGeeks\rFor\r\nGeeks\n" # display string print("Original string: ") cat(string) # string after removing new lines print("string after removing new lines: ") print(str_replace_all(string, "[\r\n]" , "")) Output: [1] "Original string: " Hello Forks Geeks [1] "string after removing new lines: " [1] "HelloGeeksForGeeks" Comment More infoAdvertise with us Next Article Count Number of Characters in String in R G gottumukkalabobby Follow Improve Article Tags : R Language R Programs R-strings R String-Programs Similar Reads 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 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 Count Number of Characters in String in R In this article, we are going to see how to get the number of characters in a string in R Programming Language. The number of characters in a string refers to the length of the string.Examples:Input: GeeksforgeeksOutput: 13Explanation: Total 13 characters in the given string.Input: Hello worldOutput 2 min read Count Number of Characters in String in R In this article, we are going to see how to get the number of characters in a string in R Programming Language. The number of characters in a string refers to the length of the string.Examples:Input: GeeksforgeeksOutput: 13Explanation: Total 13 characters in the given string.Input: Hello worldOutput 2 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 First or Last n Characters from String in R In this article, we will know how to extract the last and first char from a given string in the R programming language. For the example purpose, "Geeks for Geeks is Great" is included in our example string. Let's take a look at how we can extract the first and last n characters of this example strin 4 min read Like