1

I use this method for splitting words from string, but \n doesn't consider. How can I solve it?

public string SplitXWord(string text, int wordCount)
{
    string output = "";
    IEnumerable<string> words = text.Split().Take(wordCount);
    foreach (string word in words)
    {
        output += " " + word;
    }

    return output;
}
Trevor
  • 7,777
  • 6
  • 31
  • 50

3 Answers3

2

Well, string.Split() splits by white-spaces only

https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-6.0

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

bold is mine.

So far so good, string.Split() splits on spaces ' ', tabulation '\t', new line '\n', carriage return '\r' etc.:

Console.Write(string.Join(", ", "a\nb\rc\td e".Split()));

produces

a, b, c, d, e

If you want to split on your cown delimiters, you should prvide them:

Console.Write(string.Join(", ", "a\nb\rc\td e".Split(new char[] {' ', '\t'})));

note that \r and \n are preserved, when splitted on ' ' and 't'

a
b
c, d, e

So, it seems that your method should be something like this:

using System.Linq;

...

//DONE: static - we don't want this here
public static string SplitXWord(string text, int wordCount) {
  //DONE: don't forget about degenerated cases
  if (string.IsNullOrWhiteSpace(text) || wordCount <= 0)
    return "";

  //TODO: specify delimiters on which you want to split
  return string.Join(" ", text
    .Split(
       new char[] { ' ', '\t' },  
       wordCount + 1, 
       StringSplitOptions.RemoveEmptyEntries)
    .Take(wordCount));
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Use the overload of Split method which accepts an array of char separators and clears the empty entries

string str = "my test \n\r string \n is here"; 
string[] words = str.Split(new []{' ', '\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);

enter image description here


UPDATE:

Another solution with regex and keeping line characters:

string str = "my test\r\n string\n is here";
var wordsByRegex = Regex.Split(str, @"(?= ).+?(\r|\n|\r\n)?").ToList();

enter image description here

Georgy Tarasov
  • 1,534
  • 9
  • 11
  • i want to result be : [0] = "my" [1] = "test \n" .... – Rebin Esmaily Dec 28 '21 at 15:09
  • @RebinEsmaily please update your post to include a minimal reproducible example and expected output. Don't convey changes like this in comments, not everyone will see it and or understand your requirements. – Trevor Dec 28 '21 at 15:12
0

fiddle

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp17
{
    class Program
    {
        static void Main(string[] args)
        {
            string myStr = "hello my friend \n whats up \n bro";
            string[] mySplitStr = myStr.Split("\n");
            mySplitStr.ToList().ForEach(str=>{
                Console.WriteLine(str);
                //to remove the white spaces 
                //Console.WriteLine(str.Replace(" ",""));
            });
            Console.ReadLine();
        }
    }
}
rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47