[FAQ] C # String Parsing

Source: Internet
Author: User

Q:

How to convert the string "1. b2.c3. a4.b5. a6.c7. ac8.bc9. ac10.bd11. (1) 810 N (2) 5 m/s 12.5 times":
1. B
2. c
3.
4. B
5.
6. C
7. AC
8. BC
9. AC
10. BD
11. (1) 810 N (2) 5 m/s 12.5 times

The punctuation marks of the numbers are English characters, and other punctuation marks are Chinese characters.

Refer: http://bbs.csdn.net/topics/390534946


A:

Use Traditional Character Parsing to solve the problem. Of course, using regular expressions will be simpler.

Class program {static void main (string [] ARGs) {// The text to be parsed string TXT = "1. b2.c3. a4.b5. a6.c7. ac8.bc9. ac10.bd11. (1) 810 N (2) 5 m/s 12.5 times "; // defines a stack to store the sub-String Stack <string> words = new stack <string> (); // define the cursor int start = TXT. length-1; int end = start; // parse text forward from the end of the string to reduce the number of cursor moves while (Start> 0) {If (txt [start] = '. ') {While (start --> 0) {If (txt [start] <'0' | TXT [start]> '9') break;} words. push (txt. substring (start + 1, end-Start); End = start;} start --;} // output result while (words. count> 0) {console. writeline (words. pop ());}}}

Output:

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.