Wordwrap a Very Long String

wordwrap a very long string

This question has been asked here before:

  • Who has solved the long-word-breaks-my-div problem? (hint: not stackoverflow)
  • word wrap in css / js
  • CSS: how can I force a long string (without any blank) to be wrapped in XUL and/or HTML
  • CSS overflow with long URL

Long story short:

As far as CSS solutions go you have: overflow: scroll; to force the element to show scrollbars and overflow:hidden; to just cut off any extra text. There is text-overflow:ellipsis; and word-wrap: break-word; but they are IE only (break-word is in the CSS3 draft, however, so it will be the solution to this 5 years from now).

Bottom line is that if it is very important for you to stop this from happening with wrapping the text over to the next line the only reasonable solution is to inject ­ (soft hyphen), <wbr> (word break tag), or (zero-width space, same effect as ­ minus hyphen) in your string server side. If you don't mind Javascript, however, there is the hyphenator which seems to be pretty solid.

How to wordwrap a long string in ion-item

For ionic 1:

Add item-text-wrap class to item.

<ion-item class="item-text-wrap">
some long string
</ion-item>

For ionic 2:

Add text-wrap attribute to item.

<ion-item text-wrap>
some long string
</ion-item>

How can I force a long string without any blank to be wrapped?

for block elements:

<textarea style="width:100px; word-wrap:break-word;">  ACTGATCGAGCTGAAGCGCAGTGCGATGCTTCGATGATGCTGACGATGCTACGATGCGAGCATCTACGATCAGTC</textarea>

A good way to make long strings wrap to newline?

You could use textwrap module:

>>> import textwrap
>>> strs = "In my project, I have a bunch of strings that are read in from a file. Most of them, when printed in the command console, exceed 80 characters in length and wrap around, looking ugly."
>>> print(textwrap.fill(strs, 20))
In my project, I
have a bunch of
strings that are
read in from a file.
Most of them, when
printed in the
command console,
exceed 80 characters
in length and wrap
around, looking
ugly.

help on textwrap.fill:

>>> textwrap.fill?

Definition: textwrap.fill(text, width=70, **kwargs)
Docstring:
Fill a single paragraph of text, returning a new string.

Reformat the single paragraph in 'text' to fit in lines of no more
than 'width' columns, and return a new string containing the entire
wrapped paragraph. As with wrap(), tabs are expanded and other
whitespace characters converted to space. See TextWrapper class for
available keyword args to customize wrapping behaviour.

Use regex if you don't want to merge a line into another line:

import re


strs = """In my project, I have a bunch of strings that are.
Read in from a file.
Most of them, when printed in the command console, exceed 80.
Characters in length and wrap around, looking ugly."""

print('\n'.join(line.strip() for line in re.findall(r'.{1,40}(?:\s+|$)', strs)))

# Reading a single line at once:
for x in strs.splitlines():
print '\n'.join(line.strip() for line in re.findall(r'.{1,40}(?:\s+|$)', x))

output:

In my project, I have a bunch of strings
that are.
Read in from a file.
Most of them, when printed in the
command console, exceed 80.
Characters in length and wrap around,
looking ugly.

php how to wrap a very long word that has no spaces

Use CSS. Don't complicate things...

.rectangle {    width: 200px;    background: red;    border: 1px solid #000;    word-break: break-all;}
<div class="rectangle">    <p>DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD</p></div>

word wrapping long strings

I tried this. It seemed to work. Trial and error make me come up with this 289px width. You will still have to do something similar to what you are currently doing if you have words that are really long like this: reallyreallyreallyreallyreallyraellyreallylong

My solution is below:

setText("<html><table style='width:100%'>" + "<tr><td style=\"width:289px;\"><b>" + value + "</b></td></tr>" + "<tr><td style='font-weight:normal;font-size:11;padding-top:-50px;margin-top:-5px;'><i>" + "this is going to have to be pretty long actually" + "</i></td></tr>" + "</table>");

Is this something like what you were looking for?

Angular: How to both word wrap and line break a long div string

Use innerHTML to prevent displays the raw <br> tag.

<div id='messages' [innerHTML]=messages></div>

Smarter word-wrap in PHP for long words?

I've had a go at the custom function for this smart wordwrap:

function smart_wordwrap($string, $width = 75, $break = "\n") {
// split on problem words over the line length
$pattern = sprintf('/([^ ]{%d,})/', $width);
$output = '';
$words = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

foreach ($words as $word) {
if (false !== strpos($word, ' ')) {
// normal behaviour, rebuild the string
$output .= $word;
} else {
// work out how many characters would be on the current line
$wrapped = explode($break, wordwrap($output, $width, $break));
$count = $width - (strlen(end($wrapped)) % $width);

// fill the current line and add a break
$output .= substr($word, 0, $count) . $break;

// wrap any remaining characters from the problem word
$output .= wordwrap(substr($word, $count), $width, $break, true);
}
}

// wrap the final output
return wordwrap($output, $width, $break);
}

$string = 'hello! too long here too long here too hwordwrap a very long string How to wordwrap a long string in ion-item How can I force a long string without any blank to be wrapped? A good way to make long strings wraeeeeeereisaverylongword but these words are shorterrrrrrrrrrrrrrrrrrrr';
echo smart_wordwrap($string, 11) . "\n";

EDIT: Spotted a couple of caveats. One major caveat with this (and also with the native function) is the lack of multibyte support.



Related Topics



Leave a reply



Submit