If you don't talk nonsense, go to the topic:
The question is: is it such a string, "dfsdg <2434>, dgdfg <35346>, dtr35 <3w543>", extract content from "<" and ">?
To solve this problem, we should first group them and then judge the positions of the angle brackets. After finding them, we can extract them. So I used the two methods proposed previously to implement them as follows:
1. Source string truncation method. The sample code is as follows:
Copy codeThe Code is as follows:
String str = "dfsdg <2434>, dgdfg <35346>, dtr35 <3w543> ";
String [] strGroup = str. Split (',');
Foreach (var s in strGroup)
{
Int left = s. IndexOf ('<');
Int right = s. LastIndexOf ('> ');
S. Substring (left + 1, right-left-1 );
Console. WriteLine (s );
}
The running result is as follows:
2434
35346
3w543
2. Application Language Integrated Query (LINQ). The sample code is as follows:
Copy codeThe Code is as follows:
String str = "dfsdg <2434>, dgdfg <35346>, dtr35 <3w543> ";
Var result = from s in str. Split (',')
Let left = s. IndexOf ('<')
Let right = s. LastIndexOf ('> ')
Select s. Substring (left + 1, right-left-1 );
Result. Dump ();
The running result is as follows:
The second debugging tool uses LINQPad for debugging. For the let keyword in the second type, the help document is explained as follows:
You can use the let keyword to create a new range variable and use the result of your provided expression to initialize the variable. Once this range variable is initialized with a value, it cannot be used to store other values. However, if the range variable stores the queryable type, you can query it.
Now, I have summarized this question. I hope it will be helpful to others. You are also welcome to have better methods and ideas to solve this problem. Please leave a message, I am very grateful!