PassionateDeveloper
PassionateDeveloper

Reputation: 15138

Cut long words in c#

I have to cut any string that is longer than length x in string with "xxx- xxx" to make a line break.

So as example: with 20 chars it is okay, but when there are 30 chars in my word I must cut it to 18 + "- " + rest.

I wrote this method which ends up in an endless loop:

 string temp = s;
        string tempResult = "";
        bool found = false;
        do
        {
            found = false;
            if (s.Length < lenght) return s;
            else
            {
                //Examine every word to cut everything into words
                string[] tempList = temp.Split(' ');
                foreach (string temp2 in tempList)
                {
                    //Check every word length now,
                    if (temp2.Length > lenght)
                    {
                        tempResult = tempResult + " " + temp2.Substring(0, lenght - 3) + "- " + temp2.Substring(lenght);
                        found = true;
                    }
                    else
                        tempResult = tempResult + " " + temp2;
                }
                if (found) temp = tempResult;
            }
        } while (found);

        return tempResult;

Upvotes: 0

Views: 682

Answers (6)

Flavia Obreja
Flavia Obreja

Reputation: 1237

Although your solution is not the best, in order to fix it, you have to add the tempResult = "" at the beginning of the do-while statement. Also, make sure you also change the temp2.Substring(lenght) into temp2.Substring(lenght -3) and trim your final string as it has white spaces at the beginning:

string temp = s;
        string tempResult = "";
        bool found = false;
        do
        {
                tempResult = "";
                found = false;
                if (s.Length < lenght) return s;
                else
                {
                    //Examine every word to cut everything into words
                    string[] tempList = temp.Split(' ');
                    foreach (string temp2 in tempList)
                    {
                        //Check every word length now,
                        if (temp2.Length > lenght)
                        {
                            tempResult = tempResult + " " + temp2.Substring(0, lenght - 3) + "- " + temp2.Substring(lenght -3);
                            found = true;
                        }
                        else
                            tempResult = tempResult + " " + temp2;
                    }
                    if (found) temp = tempResult;
                }
            } while (found);

            return tempResult.TrimStart();

You could simplify your solution and loop only though the long words and not build the whole string over and over again:

 protected string test() {
            string s = "this is a test for realllllyyyyreallllyyyyloooooooongword";
            string temp = s;
            int lengthAllowed = 18;
            string tempResult = "";
            string temp3 = "";
            if (s.Length < 18) return s;
            else
            {
                //Untersuche jedes Wort, dazu schneide alles in Wörter
                string[] tempList = temp.Split(' ');
                foreach (string temp2 in tempList)
                {
                    temp3 = temp2;
                    //Jetzt jedes Wort auf Länge prüfen,
                    while (temp3.Length > lengthAllowed)
                    {
                        tempResult = tempResult + temp3.Substring(0, lengthAllowed - 3) + "- ";
                        temp3 = temp3.Substring(lengthAllowed - 3);
                    }
                    tempResult = tempResult + temp3 + " ";
                }
            }
            return tempResult.Substring(0,tempResult.Length-1);
        }

This is based on the assumption that the issue is that if we have this string:

this is a test for realllllyyyyreallllyyyyloooooooongword

The result to be:

this is a test for realllllyyyyrea- llllyyyyloooooo- oongword

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109732

I'm not really sure what your requirement is. I'm assuming that it is this:

Given a string which contains zero or more words which are separated by spaces, insert spaces such that no word in the string is longer than a specified number of characters.

The following method implements that requirement:

public string SplitLongWords(string text, int maxWordLength)
{
    var result = new StringBuilder();
    int currentWordLength = 0;

    foreach (char c in text)
    {
        if (char.IsWhiteSpace(c))
        {
            currentWordLength = 0;
        }
        else if (currentWordLength == maxWordLength)
        {
            currentWordLength = 1;
            result.Append(' '); // Or .Append('-') to separate long words with '-'
        }
        else
        {
            ++currentWordLength;
        }

        result.Append(c);
    }

    return result.ToString().TrimEnd();
}

So that given this input:

A AB ABC ABCD ABCDE ABCDEF ABCDEFG ABCDEFGH ABCDEFGHI ABCDEFGHJ
12345678901234567890

The output will be:

A AB ABC ABCD ABCD E ABCD EF ABCD EFG ABCD EFGH ABCD EFGHI ABCD EFGHJ
1234 5678 9012 3456 7890

Upvotes: 1

DGibbs
DGibbs

Reputation: 14618

What you are asking is very unclear however, I think this may be what you want and is much simpler:

static void Main(string[] args)
{
    string foo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit ligula dolor, tristique.";
    Console.Write(Truncate(foo, 20));
    Console.Read();
}

public static string Truncate(string text, int length)
{
    int index = text.Length;
    while (index > 0)
    {
        text = text.Insert(index, "- ");
        index -= length;
    }

    return text;
}

This gives:

Lorem ipsum dol- or sit amet, consect- etur adipiscing elit- . Donec blandit ligu- la dolor, tristique.-

Alternatively, this gives a different effect since it isn't clear what you need:

static void Main(string[] args)
{
    string foo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit ligula dolor, tristique.";
    Console.Write(Truncate(foo, 20));
    Console.Read();
}

public static string Truncate(string text, int maxlength)
{
     maxlength = maxlength - 2;//allow space for '- '
     string truncated = string.Empty;
     int lastSpace = 0;
     if (text.Length > maxlength)
     {
         string temp = text.Substring(0, maxlength);
         lastSpace = temp.LastIndexOf(" ");
         truncated = temp.Substring(0, lastSpace);        
     }
     else
     {
         return text;
     }
     return truncated.Trim().Insert(truncated.Length, "- ") + text.Substring(lastSpace);
}

Gives:

Lorem ipsum dolor- sit amet, consectetur adipiscing elit. Donec blandit ligula dolor, tristique.

Upvotes: 1

I4V
I4V

Reputation: 35363

How about writing an extension method to String (Taking the word boundries into consideration)

var s = "abcd defghi abcd defghi".LimitTo(10);

public static string LimitTo(this string s, int maxLen)
{
    string toEnd = "...";

    if (s.Length > maxLen)
    {
        maxLen -= toEnd.Length;
        while (!char.IsWhiteSpace(s[maxLen])) maxLen--;
        s = s.Substring(0, maxLen) + toEnd;
    }
    return s;
}

Upvotes: 2

johnnynotsolucky
johnnynotsolucky

Reputation: 540

Try something a bit simpler:

string test = s; //Your string
int count = (int)Math.Floor((decimal) test.Length / 20);

for (int i = 0; i < count; i++)
{
    test = test.Insert(((i + 1) * 20), "- ");            
}

Note: This is a basic example, it simply adds "- " to the string every 20 characters.

Edit-

If you simply want to splice the string after the first 20 characters:

s = s.Length > 20 ? s.Insert(18, "- ") : s;

Upvotes: 1

Ohlin
Ohlin

Reputation: 4178

By making an extension method out of it you can easily cut any string at any time to any length.

public static class MyExtensions
{
    public static string CutStringAt(this string s, int length)
    {
        int len = s.Length;
        if (len > length)
        {
            int pos = 0;
            StringBuilder sb = new StringBuilder();
            while (pos < len)
            {
                if ((len - pos) < length)
                {
                    int left = len - pos;
                    sb.AppendLine(s.Substring(pos, left).Trim());
                    pos += left;
                }
                else
                {
                    sb.AppendLine(s.Substring(pos, length).Trim());
                    pos += length;
                }
            }
            s = sb.ToString();
        }
        return s;
    }
}

Using this code you can cut any string easily by just calling

string aCutString = "This string is waaaaaay tooooo looong".CutStringAt(20);

Upvotes: 1

Related Questions