Use split in C # To separate strings and then use Spaces

Source: Internet
Author: User

Sometimes we use string [] lines = system. Io. file. readalllines (@ "C:/root. ini", encoding. Default); to obtain information in the text.
However, because the text file may be nonstandard, the retrieved string array contains spaces.
However, sometimes Strings need to be compared or output, so these spaces need to be processed.

 

Case 1: If the string array contains spaces
For example, a [0] = "AA"; A [1] = "; A [2] =" 321 ";

Solution: When retrieving the string array, use removeemptyentries to process the blank string,
Example: String [] Split (char [] separator, stringsplitoptions.Removeemptyentries)

Case 2: The obtained string has spaces at both ends.
For example, a [0] = "AAA"; A [1] = "ad"; A [2] = "DDD ";

Solution: We can use the trim method to solve this problem, traverse the entire array, or specify the element to use
Example: String S2 = A [0].Trim;

==============================================

The processing of strings in C # is very powerful. = A Lot Of functions ....

For more information about the split method, see msdn ---- split (Bilingual)
For more trim method reference: msdn ---- trim (http://msdn.microsoft.com/zh-cn/library/system.string.trim (vs.80). aspx)

My everybody space: http://www.renren.com/profile.do? Id = 252260104

My csdn space: http://student.csdn.net /? 1300560

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.