vb.net 删除字符串中的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25737338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove a word in a String
提问by Kramned
I have problem in removing word in my array because it is a generated GUID. And now I need is to remove 1 of word or item in that string.
我在删除数组中的单词时遇到问题,因为它是生成的 GUID。现在我需要删除该字符串中的 1 个单词或项目。
Here is my String:
这是我的字符串:
Dim guid_id as string ='3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'
Q: How can i remove the word or string " '3a112w3s-73b2-11e0-8670-88006707ed92' "in that 1 whole string?
I have idea that I need to convert it into List(of String) but I don't know how to remove it in that list.
问:如何删除" '3a112w3s-73b2-11e0-8670-88006707ed92' "整个字符串中的单词或字符串?我知道我需要将它转换为 List(of String) 但我不知道如何在该列表中删除它。
回答by Shell
You can simply find and replace the string using Replacefunction
您可以使用Replace函数简单地查找和替换字符串
Dim guid_id as string = "'3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'"
Dim strRemove As String = "'3a112w3s-73b2-11e0-8670-88006707ed92'"
guid_id = guid_id.Replace(strRemove, "").Trim()
If guid_id.Subtring(0,1) = "," Then guid_id = guid_id.Substring(1);
If guid_id.Subtring(guid_id.Length-1) = "," Then guid_id = guid_id.Substring(0, guid_id.Length-1);
回答by Mustafa
.NET Framework has an engine for text processing, which is represented by the System.Text.RegularExpressions.Regex. You can use it to replace a specific word in a string. Try this code:
.NET Framework 有一个文本处理引擎,由 System.Text.RegularExpressions.Regex 表示。您可以使用它来替换字符串中的特定单词。试试这个代码:
Dim guid_id As String = "'3a0eed1f-73b2-11e0-8670-88006707ed92','3a125s34-73b2-11e0-8670-88006707ed92','3a112w3s-73b2-11e0-8670-88006707ed92'"
Dim strRemove As String = "'3a112w3s-73b2-11e0-8670-88006707ed92'"
'To remove strRemove from string, we use Regex replace method
Dim regex = New Regex(strRemove, RegexOptions.IgnoreCase)
guid_id = regex.Replace(guid_id, "")
'Now we remove 'Comma' from string, if it is needed.
If guid_id.Subtring(0, 1) = "," Then guid_id = guid_id.Substring(1)
If guid_id.Subtring(guid_id.Length - 1) = "," Then guid_id = guid_id.Substring(0, guid_id.Length - 1)

