简体   繁体   中英

C# Splitting retrieved string to list/array

So I fetch a string from a website via code from another question I posted here. This works really well when I put it into a rich textbox, but, now I need to split the string into seperate sentences in a list/array (suppose list will be easier, since you don't need to determine how long the input is going to be).

Yesterday I found the following code at another question (didn't note the question, sorry):

List<string> list = new List<string>(Regex.Split(lyrics, Environment.NewLine));

But the input is now spliting into two parts, the first three sentences and the rest.

I retrieve the text from musixmatch.com with the following code (added fixed url for simplicity):

var source = "https://www.musixmatch.com/lyrics/Krewella/Alive";
var htmlWeb = new HtmlWeb();
var documentNode = htmlWeb.Load(source).DocumentNode;

var findclasses = documentNode
    .Descendants("p")
    .Where(d => d.Attributes["class"]?.Value.Contains("mxm-lyrics__content") == true);

var text = string.Join(Environment.NewLine, findclasses.Select(x => x.InnerText));

More information about this code can be found here . What it does in a nutshell is it retrieves specific html that has the lyrics in it. I need to split the lyrics line by line for a synchronization process that I'm building (just like was built-in in Spotify a while ago). I need something (preferably an list/array) that I can index because that would make the database to store all this data a bit smaller. What am I supposed to use for this process?

Edit: Answer to the mark of a possible duplicate: C# Splitting retrieved string to list/array

您可以将两者分开:

var lines = string.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

What I would do is to ensure that there is a common concept of "NewLine" in the code. It could be \\r, \\n or \\r\\n. Simply replace all '\\n' with "". (Edited this one)

Now, all you have to do is

var lyricLines = lyricsWithCommonNewLine.Split('\r')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM