我有一个文本框。文本框值为:
我的名字是$ John $ $ Developer $
我想做什么?我想只挑选和存储$___$
符号之间的单词。
我尝试使用value.Contain("")
,但我必须写完整的单词。
答案 0 :(得分:2)
您可以使用value.Split('$')
。结果数组中的每个第二个条目都是' $'。
答案 1 :(得分:2)
您可以使用正则表达式执行此操作:
using System.Text.RegularExpressions;
string sentence = "My Name is $John$ The $Developer$"; //or TextBox1.Text;
//split sentence into words and store them in a collection
var allWords = Regex.Split(sentence, @"\s+").Where(s => s != string.Empty);
//collection to store $words$
var wordsInDollarSigns = new List<string>();
//loop through all words in allWords
foreach(string word in allWords)
{
//if a word matches your requirement, add it in wordsInDollarSigns
if (Regex.IsMatch(word, @"\$(.+?)\$"))
{
wordsInDollarSigns.Add(word);
//to remove $ from the words use the following instead
wordsInDollarSigns.Add(word.Replace("$", string.Empty));
}
}
//wordsInDollarSigns now contains the matches
更新:根据您未在匹配的字词集合中存储"$"
字符的新要求,请参阅更新后的代码。
答案 2 :(得分:0)
'' Textbox Values in the Variable
valuefromTextbox = txtString.Text.ToString()
''Split the value and by Space and save it into Array.
Dim arrayofvaluefromTextboxSplitted As String() = valuefromTextbox.Split(" ")
'' Now I want to get those words from a string which are surrounded by $Anyword$...
For Each word As String In arrayofvaluefromTextboxSplitted
Dim arrayforSplittedSecondIndex As String() = Nothing
'' Store the word which is surrounded by $ sign
If Regex.IsMatch(word, "\$(.+?)\$") Then
'' This will split by $ sign and will take the 2nd(which is 1) index value of arrayselect. I want word without $$ sign
arrayforSplittedSecondIndex = word.Split("$")
word = arrayforSplittedSecondIndex(1).ToString()
'' Store the value in a variable
selectValueInlbl = word.Trim.ToString()
lblcheckSelect.Text += word & "<br/>"
arrayforSplittedSecondIndex = Nothing
End If
Next