C # string. Replace (not technical)

Source: Internet
Author: User

 

Recently processed string:

This string is available:

Stringbuilder test_string = new stringbuilder (); <br/> test_string.append ("55 10 16 6a "); 

 

If "10 10" is replaced with "10" and "10 16" is replaced with "16", the rest will remain unchanged.

Remove the spaces in test_string and use a loop. The Code is as follows:

Stringbuilder trs_string = new stringbuilder (); // assign a value to the intermediate variable trs_string <br/> trs_string = test_string; <br/> int II, curpos; <br/> curpos = 2; <br/> for (II = curpos; II <trs_string.length-2; II = II + 2) <br/>{< br/> string trs_temp = convert. tostring (trs_string); // convert stringbuilder to string <br/> string trs_compare = trs_temp.substring (II, 4); // take 2 bytes (4 characters ), used to compare <br/> If (trs_compare.equals ("1016") | trs_compare.equals ("1010 ")) // check whether transparent transmitted bytes exist <br/>{< br/> string remain_string = trs_temp.substring (II + 2, trs_string.length-II-2 ); // retrieve all bytes after transparent transmission <br/> trs_string.remove (II, trs_string.length-II); // Delete bytes after II <br/> trs_string.append (remain_string ); <br/> II + = 2; // skip one byte after 0x10 <br/>}< br/>} 

Later I found that it would be much faster to remove spaces, just call the replace method. Then I thought of a problem where the string is like this:

"55 10 10 10 10 16 6a"

Ideally processed string:

"55 10 16 6a"

String. will the Replace () method Replace "10 10" with "10" and then combine this "10" with the next "10" into "10 10 ", the result is processed as follows:
"55 16 6a" has only one "16" Left, but fortunately this situation will not happen after testing, the replace method adds the pointer after "10 10" or "10 16 ".

Conclusion: If you think about algorithms for a long time, replace does not process spaces, but you still have to cultivate them. The algorithms are a bit like internal work!

 

 

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.