如何以编程方式删除字符串后的空格?

Posted

技术标签:

【中文标题】如何以编程方式删除字符串后的空格?【英文标题】:How to programmatically remove space after string? 【发布时间】:2022-01-12 03:58:30 【问题描述】:

我希望删除字符串后面的空格,例如,我希望它是 SO MILWAUKEE.xlsx,而不是我的向量值 SO MILWAUKEE .xlsx。我对 dplyr 或 base r 中的解决方案持开放态度

 [1] "INTERLINE BRANDS.xlsx"         "POLY-AMERICA, LP.xlsx"         "SO TTI POWER EQUIPMENT .xlsx" 
 [4] "GENERAC SO.xlsx"               "SO TOTER LLC           .xlsx"  "SO IMPERIAL INDUSTRIAL .xlsx" 
 [7] "SO MTDARNOLD PRODUCTS .xlsx"   "SO KOHLER GENS.xlsx"           "RUBBERMAID CONSUMER     .xlsx"
[10] "SO MILWAUKEE           .xlsx"  "SO CINDERELLA INC      .xlsx"  "MAKITA                  .xlsx"
[13] "SO  THE TORO COMPANY   .xlsx"  "SO RUBBERMAID SPECIALTY.xlsx"  "SO AMES TRUE TEMPER , I.xlsx" 
[16] "SO POLYWOOD            .xlsx"  "ZEPENFORCER.xlsx"              "TIGER SUPPLIES          .xlsx"
[19] "INTERLINE BRANDS, INC..xlsx"   "SO MWE INVESTMENTS, LLC.xlsx"  "SO DIMPLEX NORTH AMERIC.xlsx"

【问题讨论】:

【参考方案1】:

假设您的数据在dat 中。您可以执行以下操作:

dat <- gsub("\\s+\\.xlsx", ".xlsx", dat)

【讨论】:

虽然我发现根据示例数据很难看到这种破坏,但在 .xlsx 中转义 . 可能会更具防御性,因此 . 不匹配“任何字符”(就像现在一样)。 @r2evans 好收获 (第二个/替换不需要反斜杠转义,只需要第一个。) @r2evans 没有意识到 replacement 被强制转换为字符 没有强制,所有三个参数都已经是字符。 pattern=(第一个)参数是唯一一个进行模式 matching 的参数; replacement=(第二个)参数实际上只能保存占位符(例如,\\1 用于分组/保存模式),第二个中没有使用其他正则表达式。【参考方案2】:
library(tidyverse)

data <- 
c("INTERLINE BRANDS.xlsx",         "POLY-AMERICA, LP.xlsx",         "SO TTI POWER EQUIPMENT .xlsx",
 "GENERAC SO.xlsx",               "SO TOTER LLC           .xlsx",  "SO IMPERIAL INDUSTRIAL .xlsx", 
 "SO MTDARNOLD PRODUCTS .xlsx",   "SO KOHLER GENS.xlsx",           "RUBBERMAID CONSUMER     .xlsx",
 "SO MILWAUKEE           .xlsx",  "SO CINDERELLA INC      .xlsx",  "MAKITA                  .xlsx",
 "SO  THE TORO COMPANY   .xlsx" , "SO RUBBERMAID SPECIALTY.xlsx",  "SO AMES TRUE TEMPER , I.xlsx" ,
 "SO POLYWOOD            .xlsx",  "ZEPENFORCER.xlsx",              "TIGER SUPPLIES          .xlsx",
 "INTERLINE BRANDS, INC..xlsx",   "SO MWE INVESTMENTS, LLC.xlsx",  "SO DIMPLEX NORTH AMERIC.xlsx")

str_replace(data, '\\s+\\.xlsx', '.xlsx')
#>  [1] "INTERLINE BRANDS.xlsx"        "POLY-AMERICA, LP.xlsx"       
#>  [3] "SO TTI POWER EQUIPMENT.xlsx"  "GENERAC SO.xlsx"             
#>  [5] "SO TOTER LLC.xlsx"            "SO IMPERIAL INDUSTRIAL.xlsx" 
#>  [7] "SO MTDARNOLD PRODUCTS.xlsx"   "SO KOHLER GENS.xlsx"         
#>  [9] "RUBBERMAID CONSUMER.xlsx"     "SO MILWAUKEE.xlsx"           
#> [11] "SO CINDERELLA INC.xlsx"       "MAKITA.xlsx"                 
#> [13] "SO  THE TORO COMPANY.xlsx"    "SO RUBBERMAID SPECIALTY.xlsx"
#> [15] "SO AMES TRUE TEMPER , I.xlsx" "SO POLYWOOD.xlsx"            
#> [17] "ZEPENFORCER.xlsx"             "TIGER SUPPLIES.xlsx"         
#> [19] "INTERLINE BRANDS, INC..xlsx"  "SO MWE INVESTMENTS, LLC.xlsx"
#> [21] "SO DIMPLEX NORTH AMERIC.xlsx"

由reprex package (v2.0.1) 于 2021-12-06 创建

【讨论】:

以上是关于如何以编程方式删除字符串后的空格?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式从 NSString 中删除特定字符?

删除空格后的所有字符串[关闭]

删除字符串中最后一个空格后的最后一个字符

编程去掉字符串中的多余空格

java如何去除字符串中的空格并且计算字符串中汉字的个数

day2