Summary of some methods for string range Truncation

Source: Internet
Author: User

A netizen raised the question of intercepting strings in a string two days ago. In addition to the normal string truncation method, I recommend using the LINQ method. In fact, the two are not very different. They both adopt string truncation, but the latter is much simpler and more practical than the former in writing and observation. 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:

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:

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.