Jump to content

Splitting string


mohamad
 Share

Go to solution Solved by Melba23,

Recommended Posts

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

  • Moderators
  • Solution

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

Edited by czardas
Link to comment
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!

Link to comment
Share on other sites

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

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)
Edited by mikell
Link to comment
Share on other sites

 

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