2012-09-20 101 views
0

当我在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”。

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

回答

3

你不应该自己动手。 使用SPFieldLookupValue

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

所以你必须:

cleanedupAuthor = "SP10Setup" 
authorId = 2 
+0

哇这工作得很漂亮很简单,没有必要乱用正则表达式,并替换字符......非常感谢 –

1

使用正则表达式像^\d*;#

感谢。 ^匹配字符串的开头,\d*表示匹配零个或多个数字,而;#是文字。