Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string by a newline in C#

Tags:

string

c#

I have a string like this :

SITE IÇINDE OLMASI\nLÜKS INSAA EDILMIS OLMASI\nSITE IÇINDE YÜZME HAVUZU, VB. SOSYAL YASAM ALANLARININ OLMASI.\nPROJESİNE UYGUN YAPILMIŞ OLMASI

I'm trying to split and save this string like this :

array2 = mystring.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

foreach (var str in sarray2)
{
    if (str != null && str != "")
    {
        _is.RelatedLook.InternalPositive += str;
    }
}

I also tried

Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

This obviously doesn't split my string. How can I split my string in a correct way? Thanks

like image 330
jason Avatar asked Aug 18 '16 10:08

jason


People also ask

How do I split a string into a newline?

Split String at Newline Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .

Can you split a string in C?

In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

How do you split a string with a new line character in VB net?

One can only split on a char but in most cases NewLine is two characters, Carriage Return (0x0D AKA Char 13) and Line Feed (0x0A AKA Char 10). But in other systems it's just a LF. So I simply remove all instances of the CR and split on the LF. Save this answer.


2 Answers

var result = mystring.Split(new string[] {"\\n"}, StringSplitOptions.None);

Since the new line is glued to the words in your case, you have to use an additional back-slash.

like image 72
A.B. Avatar answered Oct 17 '22 03:10

A.B.


In linqpad I was able to get it split

var ug = "SITE IÇINDE OLMASI\nLÜKS INSAA EDILMIS OLMASI\nSITE IÇINDE YÜZME HAVUZU, VB. SOSYAL YASAM ALANLARININ OLMASI.\nPROJESİNE UYGUN YAPILMIŞ OLMASI";
var test = ug.Split('\n');
test.Dump();

enter image description here

like image 3
Kixoka Avatar answered Oct 17 '22 02:10

Kixoka