split method to get value in between

[RGSS3]

Okay, so say I have this here:

foo = "%bar%"  how would I, (If I could) perform this in this way

p foo.split# Return "bar" without "%"how would I got about doing this?
If you just want to remove the '%' I would use gsub. So something like this

puts foo.gsub(/[\%]/, '')#> barWhat this would do is for each occurrence of '%' delete it and return the string.

split will return an array so you could have something like this:

foo = "%bar%"print foo.split(/[\%]/)#>["", "bar"]Another way to do it using scan:

Code:
foo = "%bar%"print foo.scan(/\w+/)#>["bar"]
Perfect, thank you!

Edit: How would I remove every other value?

foo = "4395078475897458%bar%i458340985094"how would I only take whats inside of the percents?
You could use a REGEX comparison

Code:
#If there will only be onefoo = "sadawdaweadasda%1231231%asdasda"p $1 if foo =~ /%(.*)%/ #Prints out "1231231"#If there can be multiplesfoo = "sadawdaweadasda%1231231%asdasda%1231231%" #and so onp foo.scan(/%(.*)%/) #scan returns an array of all matches#prints out ["1231231","1231231"]#You could also just use scan even for singles. I'm just used to using the first one#because I mostly use this for notetags and I haven't encountered anything yet which#requires me to have the same notetag on the same item more than once.
or find the first index of % sign and truncate it, then find the next one and truncate again

Code:
foo = "sadawdaweadasda%1231231%asdasda"foo = foo.[foo.index("%"),foo.length] #Should return the string without anything before the first %foo = foo.[0,foo.index("%")] #should return anything before the last %p foo #This should print out 1231231, if not, you could adjust the indexes as I forgot if they should have -1 or something
You could use a REGEX comparison

#If there will only be onefoo = "sadawdaweadasda%1231231%asdasda"p $1 if foo =~ /%(.*)%/ #Prints out "1231231"#If there can be multiplesfoo = "sadawdaweadasda%1231231%asdasda%1231231%" #and so onp foo.scan(/%(.*)%/) #scan returns an array of all matches#prints out ["1231231","1231231"]#You could also just use scan even for singles. I'm just used to using the first one#because I mostly use this for notetags and I haven't encountered anything yet which#requires me to have the same notetag on the same item more than once.or find the first index of % sign and truncate it, then find the next one and truncate again

Code:
foo = "sadawdaweadasda%1231231%asdasda"foo = foo.[foo.index("%"),foo.length] #Should return the string without anything before the first %foo = foo.[0,foo.index("%")] #should return anything before the last %p foo #This should print out 1231231, if not, you could adjust the indexes as I forgot if they should have -1 or something
The first 2 sorta work, but not the way i need it to

And for the last part I get an error:

rH6o5cv.png
What did it print out? And what exactly do you need it to do? you said you only want to get the values inside the %%

If you want foo to change, then just do

Code:
foo = $1 if foo =~ /%(.*)%/
For the last one, remove the period, it should be foo[] rather than foo.[]. Sorry
I'm trying to store the split version of the string inside of a variable.
What did it print out? And what exactly do you need it to do? you said you only want to get the values inside the %%

If you want foo to change, then just do

foo = $1 if foo =~ /%(.*)%/
In that method foo returns 0

EDIT: Did not mean to double post whoops x.x
foo.scan(/(?!\D)(\d)./)This will get you an array of every other number character starting at the first character letters/symbols are ignored.

foo.scan(/(?!\D).(\d)/)This will get you an array of every other number character starting at the second character.

Engr's solution for returning whats inside of the percent symbols should work.

Code:
foo = $1 if foo =~ /%(.*)%/
yeah it should be working, I tested it also in rubular and it returns the numbers inside the %%...


Just curious, what's the actual string you're trying to check?
Whoops, I had the "%%" wrapped around 2 words... It works perfectly thank you very much

and the string was random numbers wrapped around the player's name, for a separate function i'm working on.

Just curious, what's the actual string you're trying to check?
WimUpxN.png


The player name will be where it says %insert name here%
Oh... some sort of online system. 🙂
Hey worked out a different solution to remove every other number as this actually removes them without storing them in an array (quite a bit more complex though):

Code:
string = "54845121%hello%456656" #base stringfoo = string.slice!(/(%(.*)%)/)  #percents and text between them removed and storedz = 0                            # var for skipping every other numbernumber_s = ""                    #number stringstring.each_char {|i|  if z % 2 === 0                 #if a number is divisible by two do next part  number_s << i                  #concat char to the number string  end  z += 1}puts number_s                    #returns '5852465'puts foo.gsub!(/%/, '')          #remove percent marks from foo returns 'hello'

Original thread: https://forums.rpgmakerweb.com/threads/split-method-to-get-value-in-between.42205/