我有一个多行文本框,用户可以输入他想要的任何内容,例如
“我的名字是#Konstantinos,我20岁#”
现在我想按下按钮时输出将是#Konstantinos和#years -
是否可以使用子字符串或任何其他想法来完成?
提前谢谢
答案 0 :(得分:3)
如果你想要的只是整个字符串中的HashTags(#)
,那么你可以执行简单的.Split()
和Linq。试试这个:
<强> C#强>
string a = "Hello my name is #Konstantinos and i am 20 #years old";
var data = a.Split(' ').Where(s => s.StartsWith("#")).ToList();
<强> VB 强>
Dim a As String = "Hello my name is #Konstantinos and i am 20 #years old"
Dim data = a.Split(" ").Where(Function(s) s.StartsWith("#")).ToList()
答案 1 :(得分:1)
使用regex
会为您提供更大的灵活性。
您可以定义一个模式来搜索以#。
开头的字符串.Net正则表达式cheat sheet
Dim searchPattern = "#(\S+)" '\S - Matches any nonwhite space character
Dim searchString = "Hello my name is #Konstantinos and i am 20 #years old"
For Each match As Match In Regex.Matches(searchString, searchPattern, RegexOptions.Compiled)
Console.WriteLine(match.Value)
Next
Console.Read()
答案 2 :(得分:0)
这会奏效。试试这个..
string str = "Hello my name is #Konstantinos and i am 20 #years old asldkfjklsd #kumod";
int i=0;
int k = 0;
while ((i = str.IndexOf('#', i)) != -1)
{
string strOutput = str.Substring(i);
k = strOutput.IndexOf(' ');
if (k != -1)
{
Console.WriteLine(strOutput.Substring(0, k));
}
else
{
Console.WriteLine(strOutput);
}
i++;
}