6

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

9
  • 1
    Is the \n in the input an actual new line or the text "\n"? Commented Aug 18, 2016 at 10:54
  • @DavidG It's new line. Commented Aug 18, 2016 at 10:55
  • 3
    If you have a newline, s.Split('\n') will work. See ideone.com/Lzj1uU. Also, you may check if a string is null or empty with string.IsNullOrEmpty(). However, in your case, I'd even use string.IsNullOrWhiteSpace() Commented Aug 18, 2016 at 10:55
  • 2
    That last line of code in your question will work fine. (though you need to assign the results to an array) Commented Aug 18, 2016 at 10:57
  • 2
    @Developer: Regex does not seem necessary. What you suggest can be achieved with str.Split(new[] {"\\n"}, StringSplitOptions.RemoveEmptyEntries). Commented Aug 18, 2016 at 11:10

4 Answers 4

16
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.

Sign up to request clarification or add additional context in comments.

3 Comments

How does this make any difference?
This is splitting on the string literal "\n" rather than on an actual newline. But that may be what the original poster needs to do.
do check if your composing email content or just log message, for email, you simply need to use <br> tag |o|
3

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

Comments

3

Convert the Literal character sequence for a new line to a string, and split by that - i.e.

string clipboardText = Clipboard.GetText();
        string[] seperatingTags = { Environment.NewLine.ToString() };
        List<string> Lines = clipboardText.Split(seperatingTags, StringSplitOptions.RemoveEmptyEntries).ToList();

Comments

3

Split by a new line is very tricky since it is not consistent and you can have multiple lines or different combinations of splitting characters. I have tried many methods including some in this thread, but finally, I came up with a solution of my own which seems to fix all the cases I came across.

I am using Regex.Split with some cleaning as follows (I have wrapped it in extension method)

public static IEnumerable<string> SplitByLine(this string str)
{
    return Regex
        .Split(str, @"((\r)+)?(\n)+((\r)+)?")
        .Select(i => i.Trim())
        .Where(i => !string.IsNullOrEmpty(i));
}

usage

var lines = "string with\nnew lines\r\n\n\n with all kind of weird com\n\r\r\rbinations".SplitByLine();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.