Friday, 14 April 2017

How to find out the starting point for each match in ruby

Say, i have a following string


string = "#Sachin is Indian cricketer. #Tendulkar is right hand batsman. #Sachin has been honoured with the Padma Vibhushan award "

I want o/p as


"#Sachin|0|7;#Tendulkar|29|10;#Sachinn|63|7;"

I tried following


 new_string = ""
string.scan(/#\S+/).each{|match| new_string+="#{match}|#{string.index(match)}|#{match.length};" }

which gives me


 "#Sachin|0|7;#Tendulkar|29|10;#Sachin|0|7;"

So how i will get the starting index of each sub-string?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...