How to wrap a very long word

I am working on a C # web application, and in one section we show user comments on small boxes. It seems that one person wrote a long chain, as a result of which the box became larger.

How to avoid long words according to container size?

For example, if the user writes the following

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

and my drawer has a shorter width

I have to make it fit.

+4
source share
5 answers

Use the css word-wrap: break-word property. This will make the long lines turn to the next.

+7
source

A common task is to check the number of characters and replace the long string with a shorter one and elipsis.

aaaaaa ...

and then, if you want, show the full text in rollover.

<div title = "aaaaaa .... aaaa"> aaaaa ... </div>

In code, you can do something like

text = allText.SubString (Min (allText.Length, 80))

And combine this with the CSS style specified in other answers.

+4
source

I like to use

 overflow:hidden; 

They are usually emissions and are usually errors.

CSS Overflow Property

+3
source

Here is a solution in C #. he spilled only a word that exceeds a given limit, and other words remain as usual.

  /// <summary> /// Word wraps the given text to fit within the specified width. /// </summary> /// <param name="text">Text to be word wrapped</param> /// <param name="width">Width, in characters, to which the text /// should be word wrapped</param> /// <returns>The modified text</returns> public static string WordWrap(string text, int width) { int pos, next; StringBuilder sb = new StringBuilder(); // Lucidity check if (width < 1) return text; // Parse each line of text for (pos = 0; pos < text.Length; pos = next) { // Find end of line int eol = text.IndexOf(Environment.NewLine, pos); if (eol == -1) next = eol = text.Length; else next = eol + Environment.NewLine.Length; // Copy this line of text, breaking into smaller lines as needed if (eol > pos) { do { int len = eol - pos; if (len > width) len = BreakLine(text, pos, width); sb.Append(text, pos, len); sb.Append(Environment.NewLine); // Trim whitespace following break pos += len; while (pos < eol && Char.IsWhiteSpace(text[pos])) pos++; } while (eol > pos); } else sb.Append(Environment.NewLine); // Empty line } return sb.ToString(); } /// <summary> /// Locates position to break the given line so as to avoid /// breaking words. /// </summary> /// <param name="text">String that contains line of text</param> /// <param name="pos">Index where line of text starts</param> /// <param name="max">Maximum line length</param> /// <returns>The modified line length</returns> private static int BreakLine(string text, int pos, int max) { // Find last whitespace in line int i = max; while (i >= 0 && !Char.IsWhiteSpace(text[pos + i])) i--; // If no whitespace found, break at maximum length if (i < 0) return max; // Find start of whitespace while (i >= 0 && Char.IsWhiteSpace(text[pos + i])) i--; // Return length of text before whitespace return i + 1; } 
+1
source

You might want to check for a soft hyphen . This is - in most cases - an invisible character that breaks the line.

0
source

Source: https://habr.com/ru/post/1301137/


All Articles