删除字符串中第一次出现的下划线

时间:2016-08-09 05:48:39

标签: r

我有一个名字的字符串,看起来像

"6302_I-PAL_SPSY_000237_001"

我需要删除第一个发生的下划线,以便它将作为

gsub

我知道QNetworkConfiguration nc; QNetworkConfigurationManager ncm; QList<QNetworkConfiguration> configsForEth,configsForWLAN,allConfigs; // getting all the configs we can foreach (nc,ncm.allConfigurations(QNetworkConfiguration::Active)) { if(nc.type() == QNetworkConfiguration::InternetAccessPoint) { // selecting the bearer type here if(nc.bearerType() == QNetworkConfiguration::BearerWLAN) { configsForWLAN.append(nc); } if(nc.bearerType() == QNetworkConfiguration::BearerEthernet) { configsForEth.append(nc); } } } // further in the code WLAN's and Eth's were treated differently allConfigs.append(configsForWLAN); allConfigs.append(configsForEth); QString MAC; foreach(nc,allConfigs) { QNetworkSession networkSession(nc); QNetworkInterface netInterface = networkSession.interface(); // these last two conditions are for omiting the virtual machines' MAC // works pretty good since no one changes their adapter name if(!(netInterface.flags() & QNetworkInterface::IsLoopBack) && !netInterface.humanReadableName().toLower().contains("vmware") && !netInterface.humanReadableName().toLower().contains("virtual")) { MAC = QString(netInterface.hardwareAddress()); break; } } 但它删除了所有下划线。感谢您的任何建议。

3 个答案:

答案 0 :(得分:3)

我们可以将sub与模式_一起使用,替换为空白("")。这将删除第一次出现的&#39; _&#39;。

sub("_", "", str1)
#[1] "6302_I-PAL_SPSY_000237_001"

注意:这将删除first _出现的start,并且不会根据位置(即字符串的str2 <- "6302_I-PAL_SPSY_000237_001" sub("_", "", str2) #[1] "6302I-PAL_SPSY_000237_001" )进行限制。

例如,假设我们有字符串

_

由于示例在开头有substring,另一个选项是substring(str1, 2) #[1] "6302_I-PAL_SPSY_000237_001"

str1 <- "_6302_I-PAL_SPSY_000237_001"

数据

1) make use of third party libraray
2) pass the pdf url to google docs and open it in webview
3) by passing intent open it into pdf application like pdfViewer etc

答案 1 :(得分:3)

gsub函数执行相同操作,删除字符串符号^ used

的开头
    x <- "_6302_I-PAL_SPSY_000237_001"

    x <- gsub("^\\_","",x)

    [1] "6302_I-PAL_SPSY_000237_001"

答案 2 :(得分:0)

这也可以用基 R 的 trimws() 来完成

string1<-"_6302_I-PAL_SPSY_000237_001"

trimws(string1, which='left', whitespace = '_')

[1] "6302_I-PAL_SPSY_000237_001"

如果我们有多个带有前导下划线的单词,我们可能必须在正则表达式中包含一个单词边界 (\\b),并使用 gsub 或 stringr::string_remove

string2<-paste(string1, string1)

string2
[1] "_6302_I-PAL_SPSY_000237_001 _6302_I-PAL_SPSY_000237_001"

library(stringr)

str_remove_all(string2, "\\b_")
> str_remove_all(string2, "\\b_")

[1] "6302_I-PAL_SPSY_000237_001 6302_I-PAL_SPSY_000237_001"
相关问题