Split a string by any number of spaces

后端 未结 2 1738
南旧
南旧 2020-11-30 04:36

I have the following string:

[1] \"10012      ----      ----      ----      ----       CAB    UNCH                    CAB\"

I want to split

相关标签:
2条回答
  • 2020-11-30 05:21

    strsplit function itself works, by simply using strsplit(ss, " +"):

    ss = "10012      ----      ----      ----      ----       CAB    UNCH                    CAB"
    
    strsplit(ss, " +")
    [[1]]
    [1] "10012" "----"  "----"  "----"  "----"  "CAB"   "UNCH"  "CAB"  
    

    HTH

    0 讨论(0)
  • 2020-11-30 05:28

    Just use strsplit with \\s+ to split on:

    x <- "10012      ----      ----      ----      ----       CAB    UNCH       CAB"
    x
    # [1] "10012      ----      ----      ----      ----       CAB    UNCH       CAB"
    strsplit(x, "\\s+")[[1]]
    # [1] "10012" "----"  "----"  "----"  "----"  "CAB"   "UNCH"  "CAB"  
    length(.Last.value)
    # [1] 8
    

    Or, in this case, scan also works:

    scan(text = x, what = "")
    # Read 8 items
    # [1] "10012" "----"  "----"  "----"  "----"  "CAB"   "UNCH"  "CAB"  
    
    0 讨论(0)
提交回复
热议问题