Jump to content

Split text without break phrases


Recommended Posts

Hi all. I need to split a text in 900 characters to send a message in small pieces. But i have problem in split without break phrases that finhished with (.)  I´m try this function from Siao. Regards.

#include <Array.au3>

$Fox = _StringChop("abcdsd",4)
_ArrayDisplay($Fox)


Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = UBound($array) - 1
Return $array
EndFunc
Edited by fxg4758
Link to comment
Share on other sites

#include <Array.au3>

Local $sString = ''
For $i = 0 To 999
    $sString &= Chr(Random(97, 122, 1))
Next
Local $aSplit = StringRegExp($sString, '[A-Za-z]{1,900}', $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($aSplit)

I also searched and found so many posts, this one stood out... though is slightly different than mine as you didn't provide an example. It's up to you to change to suit your needs.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

#include <Array.au3>

Local $sString = ''
For $i = 0 To 999
    $sString &= Chr(Random(97, 122, 1))
Next
Local $aSplit = StringRegExp($sString, '[A-Za-z]{1,900}', $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($aSplit)

I also searched and found so many posts, this one stood out... though is slightly different than mine as you didn't provide an example. It's up to you to change to suit your needs.

Hi guinness! Thanks a lot for your post!

Link to comment
Share on other sites

You're welcome. Please mark as solved if it solved your problem.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

fxg4758,

This will split a string into lines on word boundries.  You define how long the line will be...

#include <array.au3>

local $str, $ilength = 50

$str &= 'Bacon ipsum dolor sit amet chicken shank swine brisket ball tip, pancetta andouille pig capicola frankfurter bresaola corned beef biltong ribeye.' & @CRLF
$str &= "Ham beef shankle meatball pork loin hamburger. Testing can't" & @CRLF
$str &= 'Pig beef ribs brisket shankle turkey ribeye frankfurter prosciutto. Landjaeger pork chop andouille jerky, prosciutto spare ribs turducken ham' & @CRLF
$str &= 'hock tail sausage pork belly rump tri-tip strip steak tongue. Doner hamburger ribeye meatball turducken andouille ' & @CRLF
$str &= 'shankle ham t-bone pork. Brisket chicken andouille kevin rump, pork loin frankfurter. Beef pastrami beef ribs shank pork belly corned beef' & @CRLF
$str &= 'drumstick tri-tip venison ham hock ball tip.' & @CRLF

local $aRET = stringregexp(stringregexpreplace($str,'\R',' '),'.{1,' & $ilength & '} ',3)

ConsoleWrite(@error ? '@error = ' & @error : _ArrayToString($aRET,@crlf) & @CRLF)

kylomas

edit: corrected code

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Local $sString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse lectus tortor dignissim sit amet, adipiscing nec ultricies sed dolor. Cras elementum ultrices diam. Maecenas ligula massa varius a semper congue euismod non mi. Proin porttitor orci nec nonummy molestie enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa scelerisque vitae consequat in pretium a enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales.'

$n =  40 
$res = StringRegExpReplace($sString, '[^.,]{1,' & $n-1 & '}((?:,|\.)?\h|\.?$)\K', @CRLF)
Msgbox(0,"", $res)

:guitar:

Link to comment
Share on other sites

Hi. I´m trying to adapt the code, but i have a problem. Everythings works until i got the last text. The loop writes only the title and don´t exit the function. Thanks for any help.

$position = 1
$title = "example title"
$ilength = 1200 - StringLen($title)
$text = "Xxxx. Xxxedfsdfsd, sdfdsf, sdaasd: ddsdfsdf."

$width = "1024"
$height = "768"

$oIE = _IECreateEmbedded()

(...)

while 2
            $aRET = stringregexp(stringregexpreplace($text,'\R',' '),'.{1,' & $ilength & '} ',3,$position)

            If IsArray($aRET) Then

                Local $array = StringSplit($aRET[0], '.', 1)

                $element = LastElement($array)

                $newlength = StringLen($aRET[0]) - StringLen($element)

                $position = $position + $newlength

                $Fox = StringMid($aRET[0], 1, $newlength)

                ConsoleWrite($title & @CRLF & StringStripWS($Fox,1) & @CRLF)
            Else
                ExitLoop
            EndIf

WEnd


Func LastElement(ByRef Const $array)
   Return $Array[UBound($array)-1]
EndFunc ;==>$lastElement
Link to comment
Share on other sites

Use mikell's SRE as it accounts for punctuation, like this...

$sRET = StringRegExpReplace($text, '[^.,]{1,' & $ilength-1 & '}((?:,|\.)?\h|\.?$)\K', @CRLF)

kylomas

edit: As far as exiting the loop, stringregexp will always return an array (assuming the syntax is valid) therefore you have no way to exit with your current logic.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Use mikell's SRE as it accounts for punctuation, like this...

$sRET = StringRegExpReplace($text, '[^.,]{1,' & $ilength-1 & '}((?:,|\.)?\h|\.?$)\K', @CRLF)

kylomas

edit: As far as exiting the loop, stringregexp will always return an array (assuming the syntax is valid) therefore you have no way to exit with your current logic.

Hi kylomas, If i use this mikell´s SRE with stringregexp, i cannot get any text. Regards.

Link to comment
Share on other sites

Is this what you are looking for...?

#include <ie.au3>

$position = 1
$title = "example title"
$ilength = 1200 - StringLen($title)
$text = "Xxxx. Xxxedfsdfsd, sdfdsf, sdaasd: ddsdfsdf."

$width = "1024"
$height = "768"

$oIE = _IECreateEmbedded()

;(...)

while 2
            $aRET = stringregexp(stringregexpreplace($text,'\R',' '),'.{1,' & $ilength & '} ',3, $position)

            If IsArray($aRET) Then

                Local $array = StringSplit($aRET[0], '.', 1)

                $element = LastElement($array)

                $newlength = StringLen($aRET[0]) - StringLen($element)

                $position = $position + $newlength

                $Fox = StringMid($aRET[0], 1, $newlength)

                ConsoleWrite($title & @CRLF & StringStripWS($Fox,1) & @CRLF)

                exitloop  ; get out of loop
            Else
                ExitLoop
            EndIf

WEnd


Func LastElement(ByRef Const $array)
   Return $Array[UBound($array)-1]
EndFunc ;==>$lastElement

Either your requirements are changing or I am NOT following you...

kylomas

edit: Can you explain what you want the while...wend loop to do???  And a REAL life example would help...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Is this what you are looking for...?

#include <ie.au3>

$position = 1
$title = "example title"
$ilength = 1200 - StringLen($title)
$text = "Xxxx. Xxxedfsdfsd, sdfdsf, sdaasd: ddsdfsdf."

$width = "1024"
$height = "768"

$oIE = _IECreateEmbedded()

;(...)

while 2
            $aRET = stringregexp(stringregexpreplace($text,'\R',' '),'.{1,' & $ilength & '} ',3, $position)

            If IsArray($aRET) Then

                Local $array = StringSplit($aRET[0], '.', 1)

                $element = LastElement($array)

                $newlength = StringLen($aRET[0]) - StringLen($element)

                $position = $position + $newlength

                $Fox = StringMid($aRET[0], 1, $newlength)

                ConsoleWrite($title & @CRLF & StringStripWS($Fox,1) & @CRLF)

                exitloop  ; get out of loop
            Else
                ExitLoop
            EndIf

WEnd


Func LastElement(ByRef Const $array)
   Return $Array[UBound($array)-1]
EndFunc ;==>$lastElement

Either your requirements are changing or I am NOT following you...

kylomas

Hi kylomas. I need to replace the function initial because i have to use a outside site to put the text. It´s a thing that change from a day to another. Regards.

Link to comment
Share on other sites

Run your script like this.  I don't think you are returning what you expect from the SRE...

#include <ie.au3>
#include <array.au3>

$position = 1
$title = "example title"
$ilength = 1200 - StringLen($title)
$text = "Xxxx. Xxxedfsdfsd, sdfdsf, sdaasd: ddsdfsdf."

$width = "1024"
$height = "768"

$oIE = _IECreateEmbedded()

;(...)

while 2
            $aRET = stringregexp(stringregexpreplace($text,'\R',' '),'.{1,' & $ilength & '} ',3, $position)

            _arraydisplay($aRET)

            If IsArray($aRET) Then

                Local $array = StringSplit($aRET[0], '.', 1)

                $element = LastElement($array)

                $newlength = StringLen($aRET[0]) - StringLen($element)  ;   this = 0

                $position = $position + $newlength

                $Fox = StringMid($aRET[0], 1, $newlength)

                ConsoleWrite($title & @CRLF & StringStripWS($Fox,1) & @CRLF)

            Else
                ExitLoop
            EndIf

WEnd


Func LastElement(ByRef Const $array)
   Return $Array[UBound($array)-1]
EndFunc ;==>$lastElement

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Is this what you are looking for...?

#include <ie.au3>

$position = 1
$title = "example title"
$ilength = 1200 - StringLen($title)
$text = "Xxxx. Xxxedfsdfsd, sdfdsf, sdaasd: ddsdfsdf."

$width = "1024"
$height = "768"

$oIE = _IECreateEmbedded()

;(...)

while 2
            $aRET = stringregexp(stringregexpreplace($text,'\R',' '),'.{1,' & $ilength & '} ',3, $position)

            If IsArray($aRET) Then

                Local $array = StringSplit($aRET[0], '.', 1)

                $element = LastElement($array)

                $newlength = StringLen($aRET[0]) - StringLen($element)

                $position = $position + $newlength

                $Fox = StringMid($aRET[0], 1, $newlength)

                ConsoleWrite($title & @CRLF & StringStripWS($Fox,1) & @CRLF)

                exitloop  ; get out of loop
            Else
                ExitLoop
            EndIf

WEnd


Func LastElement(ByRef Const $array)
   Return $Array[UBound($array)-1]
EndFunc ;==>$lastElement

Either your requirements are changing or I am NOT following you...

kylomas

edit: Can you explain what you want the while...wend loop to do???  And a REAL life example would help...

Hi kylomas. I have a program that create a internet explorer window that enter in a site. The while.... wend is to insert text in that site and then close the window and return to the master program. I try to do a Next... For... but it cames stuck when the window is created. Regards.

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...