Jump to content
Sign in to follow this  
dickjones007

characters per line help

Recommended Posts

dickjones007

I used ChrsPerLine function from this post:

is there anyway to modify it so it doesnt split the words when it  transfers text to next line?

Share this post


Link to post
Share on other sites
water

Sure: Do it yourself.

Define what you consider to be a word separator (space, (, ), - etc.), then split the string at this separators.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
dickjones007

i know how to use stringsplit but i dont want every word to be in new line

i want space before 60th character to be splitter to new line

Share this post


Link to post
Share on other sites
water

What have you tried so far?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
dickjones007

im using this

Func ChrsPerLine(ByRef $sData, $iChrsPerLine = 4096) ; Default is 4096 characters per line.
    $iChrsPerLine = Int($iChrsPerLine)

    If $iChrsPerLine <= 0 Then $iChrsPerLine = 4096

    Local $sTemp = ''

  Local $left, $right

    While StringLen($sData) > $iChrsPerLine
    $left = StringLeft($sData, $iChrsPerLine)

    $right = StringRight($sData, $iChrsPerLine)

    If StringRight($left, StringLen($left)) <> ' ' And StringLeft($right, StringLen($right)) <> ' ' then
        $sTemp &= $left & @CRLF
        $sData = StringTrimLeft($sData, $iChrsPerLine)
    EndIf
    WEnd

    If StringLen($sData) Then
        $sTemp &= $sData & @CRLF
    EndIf

    $sData = $sTemp
EndFunc   ;==>ChrsPerLine

can it be modified to suit my needs?

Share this post


Link to post
Share on other sites
water

Something like this?

#include <Constants.au3>

Example()

Func Example()
    ; Create a random string of lower-case ASCII characters.
    Local $sString = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum."
    MsgBox($MB_SYSTEMMODAL, 'Full string', $sString)

    ; Split the string to 60 characters per line.
    $sResult = WordsPerLine($sString, 60)

    ; Display the split string.
    MsgBox($MB_SYSTEMMODAL, 'Line length 60, split at word boundary', $sResult)
EndFunc   ;==>Example

Func WordsPerLine(ByRef $sData, $iChrsPerLine = 4096) ; Default is 4096 characters per line.
    Local $iStartpos = 1, $iEndPos = $iStartpos + $iChrsPerLine - 1, $sResult = ""
    $iStringLen = StringLen($sData)
    While 1
        $sTemp = StringMid($sData, $iStartpos, $iChrsPerLine)
        If StringLen($sTemp) < $iChrsPerLine Then
            $iSearchPos = $iChrsPerLine
        Else
            $iSearchPos = StringInStr($sTemp, " ", 0, -1)
        EndIf
        If $iSearchPos = 0 Then $iSearchPos = $iChrsPerLine
        $sResult &= StringMid($sData, $iStartpos, $iSearchPos) & @CRLF
        $iStartpos = $iStartpos + $iSearchPos
        If $iStartpos > $iStringLen Then ExitLoop
    WEnd
    Return $sResult
EndFunc   ;==>WordsPerLine
  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Malkey

This appears to be close.

Example()

Func Example()
    ;----- Create test string -------
    Local $sRandWords = "is that the random words like horse cat elephant and bear"
    Local $aRandWords = StringSplit($sRandWords, " ", 3)
    Local $sMeasureCharacters = 'abcdef...1abcdef...2abcdef...3abcdef...4abcdef...5abcdef...6abcdef...70characters' & @CRLF
    Local $sString = $sMeasureCharacters
    For $i = 1 To 250
        $sString &= $aRandWords[Random(0, 10, 1)] & " "
    Next
    ;----- Ebnd of Create test string -------

    ; Newline on space before 60th character on each line.
    $sString = StringRegExpReplace($sString, "(.{0,59}\S*?)(\h)", "\1" & @CRLF)

    ; Display the split string.
    ConsoleWrite($sString & $sMeasureCharacters & @LF) ; Add character measure to end.
    MsgBox(0, '', $sString)
EndFunc   ;==>Example
Edit: You can use the MsgBox window to line up the top and bottom sixes to see where the spaces are. Edited by Malkey
  • Like 2

Share this post


Link to post
Share on other sites
water

Your example doesn't seem to work for my "Lorem ipsum" example string. The last word is put on a separate line:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
diam nonumy eirmod tempor invidunt ut labore et dolore magna
aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores
et ea
rebum.
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
czardas

I disagree. It is working but the last line contains no spaces.

i want space before 60th character to be splitter to new line

 


I see what you mean though water, the split should only occur when the remainder of the string is greater than 60 characters.

Edited by czardas

Share this post


Link to post
Share on other sites
Malkey

I believe Water has nailed it. That is, hit the solution on the head.

Share this post


Link to post
Share on other sites
water

My code is not optimized but I think for short strings it should solve the OPs problem :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
czardas

Malkey - I think you just need to put z somewhere in that regexp. Test for end of text OR space. Nice regexp BTW.

Edited by czardas

Share this post


Link to post
Share on other sites
dickjones007

works perfecly

thank you water and malkey

Share this post


Link to post
Share on other sites
Malkey

Revisited for optimization and understanding.

Example()

Func Example()
    Local $sMeasureCharacters = 'abcdef...1abcdef...2abcdef...3abcdef...4abcdef...5abcdef...6abcdef...70characters'

    ;----- Create test string -------
    Local $sRandWords = "is that the random words like horse cat elephant and bear"
    Local $aRandWords = StringSplit($sRandWords, " ", 3)
    Local $sString = ""
    For $i = 1 To 250
        $sString &= $aRandWords[Random(0, 10, 1)] & " "
    Next
    ;----- End of Create test string -------

    $sString = WordsPerLine($sString, 60)

    ; Display the split string.
    ConsoleWrite($sMeasureCharacters & $sString & $sMeasureCharacters & @CRLF)
    MsgBox(0, '', $sString)
EndFunc   ;==>Example

; Newline on space before the '$iChrsPerLine' character on each line.
Func WordsPerLine(ByRef $sData, $iChrsPerLine = 4096) ; Default is 4096 characters per line.
    Local $sTemp, $iStartpos = 0, $sResult = "", $iStringLen = StringLen($sData)
    While ($iStartpos < $iStringLen)
        $sTemp = StringMid($sData, $iStartpos, $iChrsPerLine)

        ;$sResult &= StringMid($sTemp, 1, StringInStr($sTemp, " ", 0, -1) - 1) & @LF
        ; Use either above/previous line, or, use the below/next line.
        $sResult &= StringRegExpReplace($sTemp, "\h\S*$", "") & @LF

        $iStartpos = StringLen($sResult)
    WEnd
    Return $sResult
EndFunc   ;==>WordsPerLine

Share this post


Link to post
Share on other sites
dickjones007

nicely explained

Share this post


Link to post
Share on other sites
mikell

An other way - force each line to be between 50 if possible (min) and 60 (max) characters long

Local $sString = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum et dolores et ea rebum.'

$res = StringRegExpReplace($sString, '(?:.{1,50}\S{0,10}([\h.]|$)\K)' , @crlf)
Msgbox(0,"", $res)
Edited by mikell

Share this post


Link to post
Share on other sites
Malkey

Here is a working simplified regular expression method.

Example()

Func Example()
    Local $sMeasureCharacters = 'abcdef...1abcdef...2abcdef...3abcdef...4abcdef...5abcdef...6abcdef...70characters'

    ;----- Create test string -------
    Local $sRandWords = "is that the random words like horse cat elephant and bear"
    Local $aRandWords = StringSplit($sRandWords, " ", 3)
    Local $sString = ""
    For $i = 1 To 250
        $sString &= $aRandWords[Random(0, 10, 1)] & " "
    Next
    ;----- End of Create test string -------

    ; Newline on space before the '$iChrsPerLine' character on each line.
    $sString = StringRegExpReplace($sString, "(.{1,59})(\h+|$)", "\1" & @CRLF)

    ; Display the split string.
    ConsoleWrite($sMeasureCharacters & $sString & $sMeasureCharacters & @CRLF)
    MsgBox(0, '', $sString)
EndFunc   ;==>Example
Edit: Changed reg. exp. from:-

'$sString = StringRegExpReplace($sString, "(.{0,59})(h)", "1" & @CRLF)'

to

'$sString = StringRegExpReplace($sString, "(.{1,59})(h+|$)", "1" & @CRLF)'

The last line wasn't formating correctly.

Edited by Malkey

Share this post


Link to post
Share on other sites
mikell

Right  :)  nicer than the While..Wend way, no ?

Edit

OK, with my way I should have done it like this

$res = StringRegExpReplace($sString, ".{0,59}(\h)\K", @CRLF)

Thanks for this clarification

Edited by mikell

Share this post


Link to post
Share on other sites
water

Why should a RegExp solution be "nicer" (whatever that means) than a While/WEnd solution :huh:

IMHO: If it does the job it is "nice"


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
mikell

Not nicer because it is a regex, but because it's simpler (one-liner) easily understandable and 2 times faster

... although I admit being a regex fan :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×