How to wrap long lines without spaces in HTML?

前端 未结 14 1906
旧巷少年郎
旧巷少年郎 2020-12-01 03:28

If a user types in a long line without any spaces or white space, it will break formating by going wider than the current element. Something like:

HA

14条回答
  •  星月不相逢
    2020-12-01 04:09

    based on Jon's suggestion the code that I created:

    public static string WrapWords(string text, int maxLength)
        {
            string[] words = text.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Length > maxLength) //long word
                {
                    words[i] = words[i].Insert(maxLength, " ");
                    //still long ?
                    words[i]=WrapWords(words[i], maxLength);
                }
            }
            text = string.Join(" ", words);
            return (text);
        }
    

提交回复
热议问题