Delete a substring in a string between two words

Hello,

I am looking for a way to remove a text substring in a strings between two words.

Here is an example :
Channel = "Toma and Juilie like to eat chocolate"

I would like to obtain as a result:
=> "Julie like to eat"

How can I do this?

Thank you in advance for your help.

This should help you do what you want:

There is no substring command in Ruby, but the above gives you an equivalent.

I found:

stt = "Toma and Juilie like to eat chocolate"
result = stt[/and(.*)chocolate/,1]
p result

Thanks for your help.

Ps: If I have other questions about modifying text strings, I would use this topic.

Adding a space into the pattern like this .../and (.*)...
will strip the leading<space> from the result…
Or use an alternative way to strip the leading <space> - like .strip [there are several ways…]

Another possible solution …

def substring(str, word1, word2)
  str.scan(/#{word1} (.*) #{word2}/).flatten.first
end
=> :substring

phrase = "Toma and Julie like to eat chocolate"
=> "Toma and Julie like to eat chocolate"

substring(phrase, "and", "chocolate")
=> "Julie like to eat"

The method will return nil if the substring search fails. (Ie, if one of the given words do not match in the supplied string.)


Please do not. Assign a solution to this topic. Then for other String coding challenges, search this forum category to see if the question has already been asked and if not, start a new topic.

Here is a solution using the partitioning methods. This one will return an empty string if no match is found:

def substring(str, word1, word2)
  str.partition(word1).last.rpartition(word2).first.strip
end

What is surprising about programming is that there are several methods of doing the same thing.
With my low level in Ruby I don’t know if it’s for technical reasons or if it’s to give us the choice?

Generic programming will often allow you to do the same things in different ways. And how you do it could have different results on performance for instance.

… it’s called “multi-paradigm”.

Re performance, … each method call does take a minimum amount of time.
In the first example I gave, it makes 3 method calls.
In the 2nd it makes 5 method calls.

But not all methods return (do their thing) in the same amount of time. So performance testing would be needed if your substring method will get called many times and this causes a slowdown noticeable to the end user.

Absolutely! Some programmers start off thinking “which way of coding this statement will be fastest?”. But focusing on such small details too early will usually give a misleading notion of the actual impact on performance. Only by profiling the complete code can you reliably find where the actual bottlenecks are! This is especially true when a GUI is involved, because we humans are geologically slow compared to modern computers. IN many cases a calculation is so much faster than we can perceive that even a factor of 10 speedup is undetectable.

Thank you for your answers. :wink:

This evening I may be tired because I cannot remove the slashes present in a text string.

Here is an example:
color = "red \", \ "black \", \ "Yellow \"

I would like to obtain as a result:
color = "red", "black", "Yellow"

I can delete any character or string with the delete, gsub or slice! functions but I can’t do it for the slashes.

Do you know how to do it ?

Thank you in advance for your help.

I think your struggles start with not quite understanding how the Ruby interpreter processes a double-quoted string literal found in your source code. Let me break that statement down bit by bit. A “string literal” is a text object in your source that conveys the value of a String. For example, consider the statement

myString = "this is a \"double-quoted string literal\" with embedded backslashes"

The key thing is to understand that the source code string literal doesn’t go directly into memory. Rather, the Ruby interpreter processes the source code text to create a String object in memory. The double-quote symbols convey the start and end of the string literal to the interpreter. They are not included in the data of the String itself. There are other delimiters you can also use to bound string literals, with some differences in how they cause the interpreter to process the text.

Within a double-quoted string literal the backslash character \ is special. It tells the interpreter that the next character is to be treated specially (“escaped”). In your specific case, putting a backslash before a double-quote character tells the interpreter that this double-quote character isn’t the end of the string literal, rather it is to be included as a character in the String. Like the opening and closing double-quote characters, the backslash is swallowed by the interpreter; it does not actually appear in the String object’s data!

So, in your example you can’t find those backslashes in the String because they don’t exist there. Slightly abusing modern Ruby representation of Strings in memory, you can think of the data content of the String above as

this is a "double-quoted string literal" with embedded backslashes

Notice that both the delimiting double-quotes and the backslashes are not present in memory. They exist only in the source code to guide the interpreter.

That begs the question of how you are obtaining the starting string literal. Is it in your own code? If so, you don’t need to strip out anything, just code it as

color = "\"red\", \"black\", \"Yellow\""

in the first place. Alternatively, you can use single-quote character as the delimiter because the interpreter doesn’t process backslashes in single-quote strings.

If you are getting your original String in some other way, you will need to examine what it is actually providing to know how to strip it.

Also see Ruby Primer: String literals

Thank you for your explanations slbaumgartner.

You are absolutely right the slash bars in my example do not exist like the double quotes in the string class.
For this reason it is impossible for me to catch and delete them. :laughing:

I got this string when I wanted to retrieve the value of an onclick attribute of a dynamic component:

Sketchup.active_model.selection.grep(Sketchup::ComponentInstance).each{|s| p s.get_attribute "dynamic_attributes","onclick"}

As the slash bars are not interpreted this ultimately poses no problem. :wink: