Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all characters in a string until a specific character Ruby

So what I want is to get n characters until it hits a specific character.

i have this String :

a='2.452811139617034,42.10874821716908|3.132087902867818,42.028314077306646|-0.07934861041448178,41.647538468746916|-0.07948265046522918,41.64754863599606'

How can I make it to only get to the character | , but without getting that character, and get it like this:

2.452811139617034,42.10874821716908
like image 681
CodrinM Avatar asked Aug 11 '26 15:08

CodrinM


2 Answers

You can simply do it like this:

a.split('|').first

like image 102
luka Avatar answered Aug 13 '26 04:08

luka


I'm always curious as to the performance of various options and so I took the liberty of looping through some of the approaches suggested. I named them as follows:

split_all = a.split('|').first
partition = a.partition('|').first
split_two = a.split('|', 2).first
string_brack_args = a[0,a.index('|')]
string_brack_range = a[0...a.index('|')]
gsub_regex = a.gsub(/\|.*$/, "")
plain_regex = a[/[^|]+/]

The results were as follows (slowest at top):

   user     system      total        real
gsub_regex           0.170000   0.000000   0.170000 (  0.162666)
split_all            0.110000   0.000000   0.110000 (  0.109498)
split_two            0.040000   0.000000   0.040000 (  0.041792)
partition            0.040000   0.000000   0.040000 (  0.037161)
string_brack_range   0.030000   0.000000   0.030000 (  0.034021)
plain_regex          0.040000   0.000000   0.040000 (  0.033468)
string_brack_args    0.020000   0.000000   0.020000 (  0.022455)

That's nearly an eight-fold increase in efficiency between the slowest and fastest. Even some of the little things make a huge difference. Granted, I looped through these 100_000 times, so the difference is pretty small for the one instance, but even something as simple as using using split(str, 2) vs split(str) is well over twice as fast. In fact, the faster half of the approaches listed average 3 times as fast as the slower half.