Jump to content

Splitting string


Go to solution Solved by Melba23,

Recommended Posts

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.

  Quote
#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
  • Moderators
  • Solution
Posted

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:

  Reveal hidden contents

 

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
Posted
  On 10/17/2014 at 7:20 PM, czardas said:

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!

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
Posted (edited)
  On 10/17/2014 at 8:37 PM, mikell said:

 

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...