質問

I have a multiline textbox that user may type whatever he wants to for example,

"Hello my name is #Konstantinos and i am 20 #years old"

Now i want to place a button when is pressed the output will be #Konstantinos and #years -

Is that something that can be done using substring or any other idea?

Thank you in advance

役に立ちましたか?

解決

If all that you want is HashTags(#) from the entire string, you can perform simple .Split() and Linq. Try this:

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()

他のヒント

Using regex will give you more flexibility.

You can define a pattern to search for strings starting with #.

.Net regex 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()

This will work . Try this..

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++;
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top