Jump to content

Recommended Posts

Hey,

I want to use StringSplit with whole words, but unfortunately I couldn't find a way. I mean something like:

$array = StringSplit("This is a test", "is")

; I would like to receive the following:
; $array[0] = 2
; $array[1] = "This "
; $array[2] = " a test"

 

Link to post
Share on other sites
Posted (edited)
29 minutes ago, XGamerGuide said:

I want to use StringSplit with whole words

StringSplit is not the best way to do it but the example below does return your desired result:

#include <Array.au3>

$array = StringSplit("This is a test", " is ", $STR_ENTIRESPLIT)
_ArrayDisplay($array)

It's not the best way to do it because words can be delimited by more than just spaces.  For example, this would not work correctly using the example above:  "Red is: a color." or "It is, what it is."

 

Edited by TheXman
Link to post
Share on other sites

Since your initial request was to keep the space before and after "is", you could use SRE like this :

#include <Constants.au3>
#include <Array.au3>

Local $sString = "This is a test and this is a new test"
Local $arr = StringRegExp($sString, "(.+?)(?|\bis\b|$)", 3)
_ArrayDisplay($arr)

 

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

    No registered users viewing this page.

×
×
  • Create New...