Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following string which is read in from an external system.

var test = @"Order No.: 999

Customer ID:

Name:

Comment:
Position

Count 20.80"

I want to split by new lines

var result = test.Split(new string[] { @"

" }, StringSplitOptions.None);

There are two issues the above code does not work only one value is returned, the second issue is that there are differences in the new lines, is there a way I can split this using a different method?









See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
237 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this

private static readonly Regex _getLinesRegex = new Regex("
|
|
", RegexOptions.Compiled);

The splitter Method

        /// <summary>
        /// Splits a string into lines
        /// </summary>
        /// <param name="value">The string to be slitted</param>
        /// <returns>The lines of the string</returns>
        public static string[] GetLines(this string value) => _getLinesRegex.Split(value);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...