繁体   English   中英

从字符串中删除数字

[英]Removing digits from a string

当我从SharePoint 2010中列表的“作者”列中获取列表值时,将显示以下内容:

2;#SP10Setup

现在创建该特定列表项的用户是“ SP10Setup”,现在有一种简单的方法可以在SP10Setup的开头删除“ 2;#”而不影响“ SP10Setup”中的数字吗?

我有一种方法目前正在去除所有数字

//method to strip out unnecessary digits
public static string RemoveDigits(string key)
{

return Regex.Replace(key, @"\d", "");
}

这就是我获取特定列表项值的方式:

string author = listItem["Author"].ToString();

这就是我删除不需要的数字和字符的方法:

//calling the RemoveCharacter method
string noDigitsAuthor = RemoveDigits(author);
string cleanedupAuthor = noDigitsAuthor.Replace(";", "").Replace("#", "");

我从中得到的结果是“ SPSetup”,但理想情况下,我希望结果是“ SP10Setup”。

实现这一目标的最快,最简单的方法是什么?

提前致谢

你不应该自己做。 使用SPFieldLookupValue

String author = "2;#SP10Setup";
SPFieldLookupValue lookup = new SPFieldLookupValue(author);
string cleanedupAuthor = lookup.LookupValue;
int authorId = lookup.LookupId;

因此您将拥有:

cleanedupAuthor = "SP10Setup"
authorId = 2

使用正则表达式,例如^\\d*;# ^匹配字符串的开头, \\d*表示匹配零个或多个数字,而;#是文字。

首先是“;” 和'#'没有数字。 但您可以使用Regex删除字符串中的文本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM