我希望以下列方式将字符串子集化为两个子字符串
示例:
public class Account
{
public virtual int Id { get; set; }
public virtual AccountType accountType { get; set; }
public virtual int AccountTypeId { get; set; }
public virtual string Name { get; set; }
public virtual string Code { get; set; }
public virtual string Rule { get; set; }
public virtual string Description { get; set; }
public virtual bool IsHeader { get; set; }
public virtual IList<Account> ChildAccount { get; set; }
public virtual OutputGroup outputGroup { get; set; }
public virtual int OutputGroupId { get; set; }
public virtual int HeaderId { get; set; }
}
public class OpeningBalance
{
public virtual int Id { get; set; }
public virtual Account Account { get; set; }
public virtual int AccountId { get; set; }
public virtual decimal DebetNominal { get; set; }
public virtual decimal CreditNominal { get; set; }
public virtual string Description { get; set; }
}
答案 0 :(得分:4)
?substring
功能也很有用。它与?substr
的区别在于它能够同时处理单个或多个子串:
substring(str1, 5, 20)
#[1] "GAACGCCACGTCCAAA"
substring(str1, c(5,21), c(19,21))
#[1] "GAACGCCACGTCCAA" "G"
paste(substring(str1, c(5,21), c(19,21)), collapse="")
#[1] "GAACGCCACGTCCAAG"
答案 1 :(得分:3)
我们可以使用sub
来匹配字符串开头((.{4}
)的前4个字符^
),接着是我们捕获的下一个16 {(.{16})
.*
1}})后跟其他字符(\\1
)并将其替换为捕获组的反向引用(sub("^.{4}(.{16}).*", "\\1", str1)
#[1] "GAACGCCACGTCCAAA"
)
substr/substring
我们可以通过substr(str1, 5, 20)
#[1] "GAACGCCACGTCCAAA"
.
对于第二种情况,不是捕获16个字符,而是捕获15个字符后跟一个字符((.)
),然后将下一个字符捕获为一个组(\\1\\2
)并替换为后向引用(被捕获组的sub("^.{4}(.{15}).(.).*", "\\1\\2", str1)
#[1] "GAACGCCACGTCCAAG"
)
substr
或sprintf("%s%s", substr(str1, 5, 19), substr(str1, 21, 21))
#[1] "GAACGCCACGTCCAAG"
str1 <- "AGGTGAACGCCACGTCCAAAGTTAGGTGATGCATTCAAGTT"
config.tpl