A Good Way to Make Long Strings Wrap to Newline

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.

Any way to wrap a long string in Xcode to new line?

Since you are using the Swift tag, I'll assume you're programming in swift.

Swift supports multiline strings like this:

var about: String = """
My name is Adam.
I am a software developer.
"""

This will include a newline character at the end of each line. To remove the newline character add a \ to the end of the line.

Example:

var about: String = """
My name is Adam. \
I am a software developer.
"""

Swift Reference for Multiline Strings

Wrap long lines in Python

def fun():
print(('{0} Here is a really long '
'sentence with {1}').format(3, 5))

Adjacent string literals are concatenated at compile time, just as in C. http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation is a good place to start for more info.

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>

Creating a newline in a string after 20 characters at the end of a word, Python

There may be better ways to do this but this seems to work:-

s = "A very long string which is definately more than 20 characters long"
offset = 0
try:
while True:
p = s.rindex(' ', offset, offset + 20)
s = s[:p] + '\n' + s[p + 1:]
offset = p
except ValueError:
pass

print(s)

How to break a long string and make it continue on the next line?

Use something like:

p.test {
word-break: break-all;
}

Insert line breaks in long string -- word wrap

How about this:

gsub('(.{1,90})(\\s|$)', '\\1\n', s)

It will break string "s" into lines with maximum 90 chars (excluding the line break character "\n", but including inter-word spaces), unless there is a word itself exceeding 90 chars, then that word itself will occupy a whole line.

By the way, your function seems broken --- you should replace

lineLen <- 0

with

lineLen <- wordLen[i]

Cause line to wrap to new line after 100 characters

Your line isn't breaking naturally because you don;t have any spaces in it. You can use word-wrap to force the breaks, then add a max-width to say how wide it can be.

CSS

li{
max-width:200px;
word-wrap:break-word;
}

HTML

<ul>
<li>Hello</li>
<li>How are you</li>
<li>SearchImagesMapsPla yYouTubeNewsGmailDriveMoreCalendarTranslat eMobileBooksOffersWalletShoppingBloggerFin ancePhotosVideosEven more »Account OptionsSign inSearch...</li>
<li>SearchImagesMapsPla yYouTubeNewsGmailDriveMoreCalendarTranslateMobileBooksOffersWalletShoppingBloggerFinancePhotosVideosEvenmore»AccountOptionsSigninSearch...</li>
</ul>

http://jsfiddle.net/daCrosby/ZC3zK/1/

You'd need JavaScript to count exactly 100 characters and break the line there. Check here and here for JavaScript examples.



Related Topics



Leave a reply



Submit