Jump to content

Cut sentecten after X chars


andrew2
 Share

Recommended Posts

i am looking for a way to cuut a long sentence afer X chars

i tried to count the stringlenght with StringLen

search for a whitespace and cut the string there with StringSplit but it wouldnt hel at all

does anybody has a tip for me

Link to comment
Share on other sites

i know all both functions but i need to cut the sentence after x chars and make a twoline text out of it.

short explain of my problem would be nice hehe :)

i got a textbox in a gui the textbox is used to retrieve messages from a webpage mostly just text.

the textbox has a char limit of 65 what it can display but if i got a text which is 100 chars or more

i got displaying problems and would need a scrollbar and i dont want scrollbars so cut the sentence each 65 char

and put it to the next line.

Link to comment
Share on other sites

Use StringLeft and then StringMid...

$sString = "Some long Data..."

$s65Chars = StringLeft($sString, 65)
$sAfter65Chars = StringMid($sString, 66)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Have such a function in my script SMF...

#include<GUIConstantsEx.au3>
#include<array.au3>

Local $msg

    GUICreate("My GUI",250,150)  ; will create a dialog box that when displayed is centered
    $c_label = GUICtrlCreateLabel("",20,20,200,200)
    GUICtrlSetFont(-1, 8, 400, 0, 'Lucida Console')
   
    GUICtrlSetData(-1,_String_Wrap( "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghi" _
                                  & "jklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr" _
                                  & "stuvwxyzabcdefghijklmnopqrstuvwxyz", 30))
   
    GUISetState(@SW_SHOW)       ; will display an empty dialog box

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()



Func _String_Wrap($sString_Wrap_StringToSplit = "", $iString_Wrap_NoCharacters = 20)
   
    Local $aString_Wrap_StringToSplit, $aString_Wrap_StringToSplit_Save
    If StringLen($sString_Wrap_StringToSplit) > $iString_Wrap_NoCharacters Then
        StringReplace($sString_Wrap_StringToSplit, @CRLF, @LF)
        $aString_Wrap_StringToSplit = StringSplit($sString_Wrap_StringToSplit, @LF)
        For $i = 1 To UBound($aString_Wrap_StringToSplit) - 1
            $aString_Wrap_StringToSplit_Save = ""
            While StringLen($aString_Wrap_StringToSplit[$i]) > $iString_Wrap_NoCharacters
                $aString_Wrap_StringToSplit_Save &= StringLeft($aString_Wrap_StringToSplit[$i], $iString_Wrap_NoCharacters) & @LF
                $aString_Wrap_StringToSplit[$i] = StringRight($aString_Wrap_StringToSplit[$i], StringLen($aString_Wrap_StringToSplit[$i]) - $iString_Wrap_NoCharacters)
            WEnd
            $aString_Wrap_StringToSplit[$i] = $aString_Wrap_StringToSplit_Save & $aString_Wrap_StringToSplit[$i]
        Next
        Return _ArrayToString($aString_Wrap_StringToSplit, "", 1)
    Else
        Return $sString_Wrap_StringToSplit
    EndIf
EndFunc   ;==>_String_Wrap

If you use it on a label combined with the monospace font GUICtrlSetFont($c_label, 8, 400, 0, 'Lucida Console') the relative max-size always stays the same...

Edited by KaFu
Link to comment
Share on other sites

i wrote my own little function but somehow my script stops after the first value

Func _SplitStringAfterXChars ( $i_UserText, $i_StringToSplit = "<BR>", $i_StringDelimiterForce = 1)
$i_Splitted = StringSplit($i_UserText,$i_StringToSplit,$i_StringDelimiterForce)
$i_SplitNum = 1
While $i_SplitNum <= $i_Splitted[0]
return $i_Splitted[$i_SplitNum]
$i_SplitNum = $i_SplitNum + 1
WEnd
EndFunc

my srting delimiter is a htmlcode for nextline <BR> because my script retrieves html code

my problem know if my sentence would be Hello my<BR>World my function

only return Hello my and the world is complete

missing i appretiate your help kafu your function

is working but if i want to learn i need to know what is wrong in my

function so i can to it better next time :)

Edited by andrew2
Link to comment
Share on other sites

Link to comment
Share on other sites

Help-File

Use the Return keyword to exit the function.

Return should be below the wend. Create a string in the wend like $string &= @crlf & $i_Splitted[$i_SplitNum]

id did now it looks like this

Func _SplitStringAfterXChars ( $i_UserText, $i_StringToSplit = " ", $i_StringDelimiterForce = 1)
$i_Splitted = StringSplit($i_UserText,$i_StringToSplit,$i_StringDelimiterForce)
$i_SplitNum = 1
While $i_SplitNum <= $i_Splitted[0]
$string &= @crlf & $i_Splitted[$i_SplitNum]
$i_SplitNum = $i_SplitNum + 1
WEnd
Return $string
EndFunc

i got the following error WARNING: $string possibly not declared/created yet

EDIT : Ok fixed i just declared $string = "" on the beginning and its working now :) thx kafu

Edited by andrew2
Link to comment
Share on other sites

MsgBox(0, "", _SplitStringAfterXChars("Hello my<BR>World", "<BR>"))

Func _SplitStringAfterXChars($i_UserText, $i_StringToSplit = " ", $i_StringDelimiterForce = 1)
    local $string
    $i_Splitted = StringSplit($i_UserText, $i_StringToSplit, $i_StringDelimiterForce)
    $i_SplitNum = 1
    While $i_SplitNum <= $i_Splitted[0]
        $string &= @CRLF & $i_Splitted[$i_SplitNum]
        $i_SplitNum = $i_SplitNum + 1
    WEnd
    Return $string
EndFunc   ;==>_SplitStringAfterXChars

Link to comment
Share on other sites

ok is there a way to repeat this until the end of the sentence is reached

You can use StringRegExp to get an array:

#include <Array.au3>
#include <String.au3>

$sString = _StringRepeat("Some long Data..." & @CRLF, 100)

$a65Strings = StringRegExp($sString, "(?s).{0,64}[^\s]", 3)

If IsArray($a65Strings) Then
    ConsoleWrite("[0] Lenght = " & StringLen($a65Strings[0]) & @CRLF)
    ConsoleWrite("[29] Lenght = " & StringLen($a65Strings[29]) & @CRLF)
    
    _ArrayDisplay($a65Strings)
EndIf

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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