Sign in to follow this  
Followers 0
Dracil

Get text before certain character, and after?

16 posts in this topic

If i have $obj = "Hello $Me"

How can i grab "Hello" and "Me" from that string?

Any advice?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$days = StringSplit("Sun,Mon,Tue,Wed,Thu,Fri,Sat", ",")

;$days[1] contains "Sun" ... $days[7] contains "Sat"

flag = 1, entire delimiter string is needed to mark the split

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites

Ok that looks good so far,

so if its

$days = StringSplit("Hey man $aha", " $")
MsgBox(0,"",$days[1]) MsgBox(0,"",$days[3])

It works, but the problem is it can end up without the " $"

$days = StringSplit("Hey girl", " $") 
MsgBox(0,"",$days[1]) MsgBox(0,"",$days[3])

And that wont let me run it.

Share this post


Link to post
Share on other sites

soooo....

$days[3] = "$" & $days[3]

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

$Me is already set somewhere, why StringSplit it out from somewhere else? Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

$days[3] = "$" & $days[3]

Uh what ?

$obj changes it can be

$obj = "Hello $Me" 
$obj = "Hello $Doll"
 $obj = "Hello"

When its just "Hello" it crashes with stringsplit

Share this post


Link to post
Share on other sites

You could use stringinstr to see if "$" is in the string.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

If $obj = "Hello $Me" or "Hello $Whatever" already, why does that string need to be StringSplit out of itself and set to something else? Why not just use $obj, again?

Edit: Does $obj always but only sometimes have "Hello"? If the string <> "Hello", or if it's more than 5 chars, or some other thing.., then put a StringSplit to it..

Edited by snowmaker

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

Oh, I thought you wanted the "$" back on after the stringsplit.


The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

Well if the $obj is not only "hello" if its "hello $mia" i need to get into into to seperate strings. i need the "mia" in one and "hello" in one, the "hello" can also change u see.

Share this post


Link to post
Share on other sites

StringRegExp might be the easiest way then.


The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again.

Share this post


Link to post
Share on other sites

Can i use StringRegExp to get all data before " $" and then use it again to get everything after " $" and if there is no " $" it shud just return something empty right ?

Share this post


Link to post
Share on other sites

$obj changes it can be

$obj = "Hello $Me" 
$obj = "Hello $Doll"
 $obj = "Hello"

When its just "Hello" it crashes with stringsplit

Check if StringSplit was successful:

#include <array.au3> ; for _ArrayDisplay
$obj = "Hello $Me" 
$result = StringSplit($obj," ")
If @error Then  $result = $obj
If IsArray($result) Then
    _ArrayDisplay($result)
Else
    MsgBox(0,"",$result)
EndIf

Hi ;)

Share this post


Link to post
Share on other sites

Does this work in all cases the way you need?

#include <Array.au3>
Local $str[6] = ["Hello$World!", "Hello$", "Hello", "$World!", "$", ""], $ret
For $i = 0 To UBound($str) - 1
    $ret = StringRegExp($str[$i], "([^$]*)\$?([^$]*)?", 1)
    _ArrayDisplay($ret, "Split of '" & $str[$i] & "'")
Next

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Ok that looks good so far,

so if its

$days = StringSplit("Hey man $aha"," {:content:}quot;)
MsgBox(0,"",$days[1]) MsgBox(0,"",$days[3])

It works, but the problem is it can end up without the " $")

$days = StringSplit("Hey girl", " $") 
MsgBox(0,"",$days[1]) MsgBox(0,"",$days[3])

And that wont let me run it.

For future reference, I direct your attention to the parameters of StringSplit in the help file.

You are using "0", the default flag parameter, which is wrong, because you are using more than one character as the delimiter to mark the split/s in the string.

#include <Array.au3>
$days = StringSplit("Hey man $aha", " $", 3)
_ArrayDisplay($days)

Edit: Change the the automatic change of " $" to " {:content:}quot; back to " $"

Edit2: And again.

Edited by Malkey

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Also, for your future reference, if you are using more than one character(say "xy") with default flag, string will be splitted by the delimeter "x" OR "y".

Example:

#include <array.au3>
$a = "111x222y333"
$b = StringSplit($a,"xy")
_ArrayDisplay($b)
Edited by Mison

Hi ;)

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