Sign in to follow this  
Followers 0
mohamad

Splitting string

10 posts in this topic

#1 ·  Posted (edited)

Hello guys, i want to split $sText and get the same results as displayed except  "hardalot"(hard alot),i want them to be seperate but in the same time i want sug-gestion to be combined.

#include <array.au3>
Local $sText="Test text- - ,do-what-ever-you-want ,eat-apple ,write sug-" & @CRLF & "gestion and work hard" & @CRLF & "alot"
MsgBox(0,"",$sText)
Local $aArray = StringSplit($sText, ' ')
    _ArrayDisplay($aArray)
Edited by mohamad

Share this post


Link to post
Share on other sites



Mohamed,

How about this: :)

#include <Array.au3>

Local $sText = "Test text- - ,do-what-ever-you-want ,eat-apple ,write sug-" & @CRLF & "gestion and work hard" & @CRLF & "alot"

$sText = StringReplace(StringReplace($sText, "-" & @CRLF, ""), @CRLF, " ")

Local $aArray = StringSplit($sText, " ")

_ArrayDisplay($aArray)
M23
1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

works great :thumbsup:

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Oh, I wrote something a little more complicated. ;)  It checks that the hyphen dash is part of a split word, or not, before joining. It also ignores multiple spaces and line breaks.

;

Local $sText="Test text- - ,do-what-ever-you-want ,eat-apple ,write sug-" & @CRLF & "gestion and work hard" & @CRLF & "alot"
Local $aArray = StringRegExp(StringRegExpReplace($sText, "(?i)(?<=[a-z])\-\r\n(?=[a-z])", ""), "\S+", 3)
_ArrayDisplay($aArray)

:;

Or with the item count added.

Local $sText="Test text- - ,do-what-ever-you-want ,eat-apple ,write sug-" & @CRLF & "gestion and work hard" & @CRLF & "alot"
Local $aArray = StringRegExp(StringRegExpReplace("? " & $sText, "(?i)(?<=[a-z])\-\r\n(?=[a-z])", ""), "\S+", 3)
$aArray[0] = UBound($aArray) -1
_ArrayDisplay($aArray)

;

It's the same approach as Melba23, but a little more fussy.

Edited by czardas
1 person likes this

Share this post


Link to post
Share on other sites

Oh, I wrote something a little more complicated. ;)  It checks that the hyphen dash is part of a split word, or not, before joining. It also ignores multiple spaces and line breaks.

;

Local $sText="Test text- - ,do-what-ever-you-want ,eat-apple ,write sug-" & @CRLF & "gestion and work hard" & @CRLF & "alot"
Local $aArray = StringRegExp(StringRegExpReplace($sText, "(?i)(?<=[a-z])\-\r\n(?=[a-z])", ""), "\S+", 3)
_ArrayDisplay($aArray)

:;

Or with the item count added.

Local $sText="Test text- - ,do-what-ever-you-want ,eat-apple ,write sug-" & @CRLF & "gestion and work hard" & @CRLF & "alot"
Local $aArray = StringRegExp(StringRegExpReplace("? " & $sText, "(?i)(?<=[a-z])\-\r\n(?=[a-z])", ""), "\S+", 3)
$aArray[0] = UBound($aArray) -1
_ArrayDisplay($aArray)

;

It's the same approach as Melba23, but a little more fussy.

That's amazing ,i never thought such thing could be possible. this helped me alot,thanks!

1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I'm glad you liked that version. :) AutoIt is so rich with methods and I'm often surprised by things myself. Simple solutions are usually the best and also fundamental to learning. Sometimes there are many factors to consider and a more complicated solution is required.

The code I posted is not able to differentiate between a genuine word split and a hyphenated word interrupted by a line break. There is no general solution for that, as far as I know.

Edited by czardas
1 person likes this

Share this post


Link to post
Share on other sites

Right.

For the fun AND for the apple  :)

2 people like this

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

 

For the fun  :)

#Include <Array.au3>

Local $sText="Test text- - ,do-what-ever-you-want ,eat-apple ,write sug-" & @CRLF & "gestion and work hard" & @CRLF & "alot"

$aArray = StringSplit(StringRegExpReplace(StringRegExpReplace($sText, '\R|-(?=\w)', " "), '-\h+', ""), ',')
_ArrayDisplay($aArray)

... i probably should read more about StringRegExpReplace,used to use a long code to remove "-" between words ,

thanks , this meant more than fun to me . :)

Edited by mohamad

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  
Followers 0