Create a Newline for Every X Number of Characters

How can I add a newline after X number of characters in Perl?

An even shorter option.

$m = "aaaaabbbbbcccccdd";
$m =~ s/(.{1,5})/$1\n/gs;
print $m;



Of course I think my version is the best of all presented up to now. ;)

Add line break after every 20 characters and save result as a new string

You probably want to do something like this

inp = "A very very long string from user input"
new_input = ""
for i, letter in enumerate(inp):
if i % 20 == 0:
new_input += '\n'
new_input += letter

# this is just because at the beginning too a `\n` character gets added
new_input = new_input[1:]

PHP new line every X characters in long characters sequence

chunk_split($string, 10) for more info

How can I create a new line after a set of characters in python

text = input('Enter your text: ')
list_ = text.split(' ')

if len(text) >= 16:
for i, j in zip(range(len(list_)),list_):
if i == len(list_)-1:
print(j, end=' ')
print('\n'+ j)

How to insert a new line character after a fixed number of characters in a file

How about something like this? Change 20 is the number of characters before the newline, and temp.text is the file to replace in..

sed -e "s/.\{20\}/&\n/g" < temp.txt

How to insert a newline character after every 200 characters with jQuery

Break the string after every 200 characters, add a newline, and repeat this process with the remaining string:

function addNewlines(str) {
var result = '';
while (str.length > 0) {
result += str.substring(0, 200) + '\n';
str = str.substring(200);
return result;

Adding a line break every x characters in Notepad++

You can record a macro for this on Notepad++.
I would follow these steps while recording.

  1. Place the cursor at the start.
  2. Move the cursor 70 times to the right (using the right arrow)
  3. When you are at the 70th character press the Control and the Right Arrow keys.
  4. This will make the cursor to jump to the start of the next word.
  5. Press # and Enter.

Stop the macro and play back.

Or you could use something like this. It does exactly what you ask.

Related Topics

Leave a reply