Tapatalk

Splitting a string into multiple lines on base of character count

Splitting a string into multiple lines on base of character count

1
NewbieNewbie
1

PostJul 28, 2018#1

I am trying to split a string into lines of same length. I want to split the string by counting the number of characters. If the count is 50 then whole rest of the string goes to next line. Again start counting from the beginning of second line, once reached count of 50, move rest of the string to next line and same count starts with 3rd line and so on.

For example: If I have a text file with below format
 

Code: Select all

hfcyfycfcucjccmnckhc cvshcvhfcyfycfcucjccmnckhc cvshcvm c cwujgvuewgvhiwdhvidwv vdwgvuwgdvjdbjvv hciehvidhvihdkv vihdihvidhvihviv
sbyds gvdy gvudsgvudgvjdbvjvd hgvudvjdbuvugdugvbdjsbcjsdbvgviebvjvbjdgvuegvbvjbvjvbuegvuvbjdbcweguevhvb wjvuegvvh vnbieojejgvhcvjbchevgevuvbev
Expected output:

Code: Select all

hfcyfycfcucjccmnckhc cvshcv
hfcyfycfcucjccmnckhc cvshcv
m c cwujgvuewgvhiwdhvidwv
 vdwgvuwgdvjdbjvv hciehvidh

6,825625
Grand MasterGrand Master
6,825625

PostJul 29, 2018#2

There is the command String at every increment... for such a task. It can be executed by clicking
  • on ribbon tab Edit in fourth group Insert on down arrow of first item Insert item and next clicking in opened pop-up menu on last menu item String at every increment... on using ribbon mode;
  • in menu Edit in submenu Insert item on last menu item String at every increment... on using toolbar/menu mode with contemporary menus;
  • in menu Insert on menu item String at every increment... on using toolbar/menu mode with traditional menus.
For a text file with DOS line endings (see status bar) use as string to increment ^p as suggested also in opened dialog. For a text file with UNIX line endings use ^n.

The example is more confusing than helpful. The input data contains two lines, but the expected output contains just the strings from first line. The expected output has just 27 characters by line with exception of third line which has just 25 characters. Why has this line just 25 characters?

It would have been much better to post an example with 01234 56789 abcdefghi jklmno pqrstu vwxyz making it much easier to see where line terminators should be inserted by the macro.

The two lines of input example modified by running a Perl regular expression Replace all from top of file with search string .{27}\K and \r\n as replace string (DOS line termination, i.e. carriage return + line-feed) produce as output:

Code: Select all

hfcyfycfcucjccmnckhc cvshcv
hfcyfycfcucjccmnckhc cvshcv
m c cwujgvuewgvhiwdhvidwv v
dwgvuwgdvjdbjvv hciehvidhvi
hdkv vihdihvidhvihviv
sbyds gvdy gvudsgvudgvjdbvj
vd hgvudvjdbuvugdugvbdjsbcj
sdbvgviebvjvbjdgvuegvbvjbvj
vbuegvuvbjdbcweguevhvb wjvu
egvvh vnbieojejgvhcvjbchevg
evuvbev
The same input data modified by  running a Perl regular expression Replace all from top of file with search string (?s).{27}\K and \r\n as replace string produce as output:

Code: Select all

hfcyfycfcucjccmnckhc cvshcv
hfcyfycfcucjccmnckhc cvshcv
m c cwujgvuewgvhiwdhvidwv v
dwgvuwgdvjdbjvv hciehvidhvi
hdkv vihdihvidhvihviv
sbyd
s gvdy gvudsgvudgvjdbvjvd h
gvudvjdbuvugdugvbdjsbcjsdbv
gviebvjvbjdgvuegvbvjbvjvbue
gvuvbjdbcweguevhvb wjvuegvv
h vnbieojejgvhcvjbchevgevuv
bev
(?s) at beginning of search string results in applying . also on vertical whitespaces like carriage return and line-feed and therefore counting these line ending characters like all other characters in text file.

This Perl regular expression Replace all with (?s) at beginning of search string is like using command String at every increment... with ^p as string to insert and 27 as record length.