dickjones007 Posted December 7, 2013 Share Posted December 7, 2013 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? Link to comment Share on other sites More sharing options...
water Posted December 7, 2013 Share Posted December 7, 2013 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 More sharing options...
dickjones007 Posted December 7, 2013 Author Share Posted December 7, 2013 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 Link to comment Share on other sites More sharing options...
water Posted December 7, 2013 Share Posted December 7, 2013 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 More sharing options...
dickjones007 Posted December 7, 2013 Author Share Posted December 7, 2013 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 More sharing options...
Solution water Posted December 7, 2013 Solution Share Posted December 7, 2013 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 dickjones007 1 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 More sharing options...
Malkey Posted December 7, 2013 Share Posted December 7, 2013 (edited) 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 ;==>ExampleEdit: You can use the MsgBox window to line up the top and bottom sixes to see where the spaces are. Edited December 7, 2013 by Malkey dickjones007 and czardas 2 Link to comment Share on other sites More sharing options...
water Posted December 7, 2013 Share Posted December 7, 2013 (edited) 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 December 7, 2013 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 More sharing options...
czardas Posted December 7, 2013 Share Posted December 7, 2013 (edited) 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 December 7, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Malkey Posted December 7, 2013 Share Posted December 7, 2013 I believe Water has nailed it. That is, hit the solution on the head. Link to comment Share on other sites More sharing options...
water Posted December 7, 2013 Share Posted December 7, 2013 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 More sharing options...
czardas Posted December 7, 2013 Share Posted December 7, 2013 (edited) Malkey - I think you just need to put z somewhere in that regexp. Test for end of text OR space. Nice regexp BTW. Edited December 7, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
dickjones007 Posted December 7, 2013 Author Share Posted December 7, 2013 works perfecly thank you water and malkey Link to comment Share on other sites More sharing options...
Malkey Posted December 7, 2013 Share Posted December 7, 2013 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 More sharing options...
dickjones007 Posted December 7, 2013 Author Share Posted December 7, 2013 nicely explained Link to comment Share on other sites More sharing options...
mikell Posted December 8, 2013 Share Posted December 8, 2013 (edited) 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 December 8, 2013 by mikell Link to comment Share on other sites More sharing options...
Malkey Posted December 8, 2013 Share Posted December 8, 2013 (edited) 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 ;==>ExampleEdit: 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 December 8, 2013 by Malkey Link to comment Share on other sites More sharing options...
mikell Posted December 8, 2013 Share Posted December 8, 2013 (edited) 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 December 8, 2013 by mikell Link to comment Share on other sites More sharing options...
water Posted December 8, 2013 Share Posted December 8, 2013 Why should a RegExp solution be "nicer" (whatever that means) than a While/WEnd solution 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 More sharing options...
mikell Posted December 8, 2013 Share Posted December 8, 2013 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now