-3

I want to fill a field with a string of many words. When I have huge words I need a to split the word. If my word is longer than 10 letters I need a space at position 5 of this word.

How do I get the new "myString" with shorter words?

    static void Main(string[] args)
    {
        var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
        var myStringSplit = myString.Split();
        var query = myStringSplit.Where(x => x.Length > 10).ToList();
        foreach (var item in query)
        {
            item.Insert(5, " ");
        }
    }

Edit: My expected result: myString = "This is my strin g with some loooo ooooo ooooo ng words. Pleas e devid e meeee eeeee eeeee eeeee eeeee e";

kame
  • 20,848
  • 33
  • 104
  • 159

3 Answers3

2

Here is example how you can get your result:

public static void Main(string[] args)
{
    var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
    string[] myStringSplit = myString.Split();
    for (int i =0;i<myStringSplit.Length;i++)
    {
        if(myStringSplit[i].Length>5)
            myStringSplit[i] = Regex.Replace(myStringSplit[i], ".{5}(?=.)", "$0 ");
    }
    var resultString = myStringSplit.Aggregate((x,y)=> x+" "+y);
    Console.WriteLine(resultString);
}

The output will be the following:

This is my strin g with some loooo ooooo ooooo ng words . Pleas e devid e meeee eeeee eeeee eeeee eeeee e

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • The `Regex` should be `Regex.Replace(myStringSplit[i], ".{5}(?=.)", "$0 ");` or `".{5}(?!$)"` (test with a string of 10 characters, with your regex it is `12345 67890 ` with an extra space at the end. And instead of `Aggregate` you could use `string.Join`: `string.Join(" ", myStringSplit)` – xanatos Jun 07 '17 at 12:13
0

Try this might be helpful

static void Main(string[] args)
        {
            var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
            var myStringSplit = myString.Split();
            var query = myStringSplit.Where(x => x.Length > 10).ToList();
            foreach (var item in query)
            {
                string outString = Regex.Replace(item, ".{5}", "$0 ");
            }
        }

This will separates the string after each five words..

Edited

Lifewithsun
  • 968
  • 14
  • 34
0
static void Main(string[] args)
{
    var myString = "This is my string with some loooooooooooooong words. Please devide meeeeeeeeeeeeeeeeeeeeeeeee";
    var newString = string.Empty;
    var myStringSplit = myString.Split();
    var query = myStringSplit.Where(x => x.Length > 10).ToList();
    foreach (var item in myStringSplit)
    {
        if(item.Length > 10)
        {
            var itemToSplit = item;
            itemToSplit.Insert(5, " ");
            newString += itemToSplit;
        }
        else
        {
           newString += item;
        }
    }
}

It should be something like this. The reason your attempt is not working is because you are not allowed to edit the item in the foreach loop. More info on the foreach loop

Alin
  • 394
  • 5
  • 14