开发者

Select and show characters/string between specific delimiters in C#.net

开发者 https://www.devze.com 2023-02-21 22:26 出处:网络
I have the following format in a single string: abcd*%HelloWorld;df4?*Thisis;sf4er7?Test;sdf From the above text,

I have the following format in a single string:

abcd*%HelloWorld;df4?*Thisis;sf4er7?Test;sdf

From the above text, I want to grab HelloWorld in textbox1, Thisis in textbox2 and Test in textbox3.

Each of them lies in between "%;"s and "?;"s开发者_开发问答.

I want it to happen in single event. How do i do this in C#.net? Suggest me the fastest yet easiest way to get this done!


Try this one. http://en.csharp-online.net/Manipulating_Strings_in_CSharp%E2%80%94Splitting_a_String

string input ="abcd*%HelloWorld;df4?*Thisis;sf4er7?Test;sdf"
input = input.Replace('%').Replace('?')
string[] splited = input.split(';')

Or Regex http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx

string input = "plum--pear";
string pattern = "-";            // Split on hyphens

string[] substrings = Regex.Split(input, pattern);
foreach (string match in substrings)
{
   Console.WriteLine("'{0}'", match);
}


For such a simple case I'd use String.Split - something as simple as string.Split(new string[] {"%;", "?;"}, StringSplitOptions.RemoveEmptyEntries);


I think you should first unify the splitters like replace "%" and "?" with unique character like '|' or even space.

then split the string by the unified character that you set.

finely if you know the indexes you will fetch the array of the strings that returned from the splitting process and you will get in each index a word or the phrase you want

0

精彩评论

暂无评论...
验证码 换一张
取 消