Jump to content

characters per line help


Go to solution Solved by water,

Recommended Posts

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 (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

What have you tried so far?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...