Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:42:17+00:00 2026-06-04T11:42:17+00:00

Given a character string test_1<-abc def,ghi klm test_2<-abc, def ghi klm I wish to

  • 0

Given a character string

test_1<-"abc def,ghi klm"
test_2<-"abc, def ghi klm"

I wish to obtain

"abc"
"def"
"ghi"

However, using strsplit, one must know the order of the splitting values in the string, as strsplit uses the first value to do the first split, the second to do the second… and then recycles.

But this does not:

strsplit(test_1, c(",", " "))
strsplit(test_2, c(" ", ","))

strsplit(test_2, split=c("[:punct:]","[:space:]"))[[1]]

I am looking to split the string wherever I find any of my splitting values in a single step.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:42:18+00:00Added an answer on June 4, 2026 at 11:42 am

    Actually strsplit uses grep patterns as well. (A comma is a regex metacharacter whereas a space is not; hence the need for double escaping the commas in the pattern argument. So the use of "\\s" would be more to improve readability than of necessity):

    > strsplit(test_1, "\\, |\\,| ")  # three possibilities OR'ed
    [[1]]
    [1] "abc" "def" "ghi" "klm"
    
    > strsplit(test_2, "\\, |\\,| ")
    [[1]]
    [1] "abc" "def" "ghi" "klm"
    

    Without using both \\, and \\, (note extra space that SO does not show) you would have gotten some character(0) values. Might have been clearer if I had written:

    > strsplit(test_2, "\\,\\s|\\,|\\s")
    [[1]]
    [1] "abc" "def" "ghi" "klm"
    

    @Fojtasek is so right: Using character classes often simplifies the task because it creates an implicit logical OR:

    > strsplit(test_2, "[, ]+")
    [[1]]
    [1] "abc" "def" "ghi" "klm"
    
    > strsplit(test_1, "[, ]+")
    [[1]]
    [1] "abc" "def" "ghi" "klm"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to check existing of Character (*,&,$) in Given String using python command
Given a string in the format: XXX999999v99 (where X is any alpha character and
given any string, i want to remove any letters after a specific character. this
I'm in need to modify a given string to contain only alpha numerical characters,
Is there a simple way of checking whether a given character is in a
I need to print the ASCII value of the given character in awk only.
Given a dictionary of words and an initial character. find the longest possible word
Given a file in UTF-8, containing characters in various languages, how can I obtain
I trying to do some very basic string processing in C (e.g. given a
Given an arbitrary string of text, the task is to group the text into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.