This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17437|Reply: 0

[Source] C# converts multiple consecutive spaces in the middle of a string into a single space

[Copy link]
Posted on 10/5/2015 9:39:17 PM | | |
Today I ran into this problem while helping a customer import the information from a txt file into a database. Because the number of spaces between two strings in the customer's txt file is uncertain, there is no way to use the split function to split, and the last way I came up with is to convert the consecutive spaces into a space, and then use split to split, which is easy to do.

The principle used here is: use regular expressions, so the file should be referenced

using System.Text.RegularExpressions;
(Move the mouse over the code, four icons will appear at the top of the code, the first is to view the source code, the second is to copy the code, the third is to print the code, and the fourth is help)
Suppose the contents of the string are
str="A B  C          D E F";
str = new System.Text.RegularExpressions.Regex("[\\s]+"). Replace(str, " ");
(Move the mouse over the code, four icons will appear at the top of the code, the first is to view the source code, the second is to copy the code, the third is to print the code, and the fourth is help)
That's it, put the str output to see if it's A B C D E F.





Previous:Reveal how pitting the "Wuhan Guangbutun Computer City" is! A gathering place for scammers! I have personally experienced it to warn everyone
Next:Who is the owner of Qingdao Shandehuo Seafood Barbecue Home Cooking? The boss has run away
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com