【问题标题】:Splitting a string into substrings so that each string starts with a capital letter?将字符串拆分为子字符串,以便每个字符串以大写字母开头?
【发布时间】:2018-02-03 03:27:47
【问题描述】:

我有以下字符串:

dt

我试过了 unlist(strsplit(dt, split = "[[:upper:]]"))

但它去掉了大写字母。

我想拆分它,以便每个字符串都以大写字母开头。例如, “我感觉夜晚爆炸了”,
“当我们在一起时”,
“情绪超载”等

有没有办法像这样拆分它?谢谢!

【问题讨论】:

  • 可能是关于 SO 提出的最性感的问题。

标签: r string split


【解决方案1】:

你需要使用“lookarounds”

x <- "I feel the night explode When we're together Emotion overload In the heat of pleasure Take me I'm yours into your arms Never let me go Tonight I really need to know Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name The passion's so complete It's never ending As long as I receive This message you're sending Body to body, soul to soul Always feel you near So say the words I long to hear Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name Love...love on the run Breaking us down Though we keep holding on I don't want to lose No...I can't let you go... Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name Tell it to my heart Tell me from the stars Tell it to my heart Tell it to my heart Tell me from the stars Tell it to my heart Never make it stop Oh take it to the heart Oh no no ah ah Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name"
v <- unlist(strsplit(x, split = "(?=\\s[A-Z])", perl=TRUE))
v <- v[v!=" "]

head(v)
# [1] "I feel the night explode" "When we're together"     
# [3] "Emotion overload"         "In the heat of pleasure" 
# [5] "Take me"                  "I'm yours into your arms"

【讨论】:

    【解决方案2】:
    ooh_yeah_baby_baby <- "I feel the night explode When we're together Emotion overload In the heat of pleasure Take me I'm yours into your arms Never let me go Tonight I really need to know Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name The passion's so complete It's never ending As long as I receive This message you're sending Body to body, soul to soul Always feel you near So say the words I long to hear Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name Love...love on the run Breaking us down Though we keep holding on I don't want to lose No...I can't let you go... Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name Tell it to my heart Tell me from the stars Tell it to my heart Tell it to my heart Tell me from the stars Tell it to my heart Never make it stop Oh take it to the heart Oh no no ah ah Tell it to my heart Tell me I'm the only one Is this really love or just a game Tell it to my heart I can feel my body rock Every time you call my name"
    
    unlist(stringr::str_split(ooh_yeah_baby_baby, "(?=\\p{Upper})"))[-1]
    

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 2011-11-25
      • 1970-01-01
      • 2014-06-29
      • 2014-12-02
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      相关资源
      最近更新 更多