Extract substring from index 0 up to searched character?

The following will split the string on spaces, and will output the first element (in your case, this would be the first name) user.name. Split0.

The following will split the string on spaces, and will output the first element (in your case, this would be the first name). User.name. Split0.

I would say: s. Split0 # s = user. Name or s.split.

First These both split the string on whitespace into an array of strings and return the first element. It will still work even if only a single name is given instead of both first and last.

$` is precisely what you're looking for. "John Doe" =~ / / $` # => "John" It's also faster than the alternatives listed in Knut's answer: require 'benchmark' TEST_LOOPS = 10_000_000 NAME = 'Jon Doe the third' #~ p NAME. Split0 #~ p NAME.split.

First #~ p NAME/^\S*/ #~ p NAME. Split(/\s/, 2). First #~ p NAME.

Split(/\s/, 2)0 #~ p NAME. Split(' ', 2)0 #~ exit Benchmark. Bmbm(10) {|b| b.

Report('0') { TEST_LOOPS. Times { NAME. Split0 } #Testloops } #b.

Report b. Report('02regex') { TEST_LOOPS. Times { NAME.

Split(/\s/, 2)0 } #Testloops } #b. Report b. Report('02string') { TEST_LOOPS.

Times { NAME. Split(' ', 2)0 } #Testloops } #b. Report b.

Report('first') { TEST_LOOPS. Times { NAME.split. First } #Testloops } #b.

Report b. Report('first2regex') { TEST_LOOPS. Times { NAME.

Split(/\s/, 2). First } #Testloops } #b. Report b.

Report('first2string') { TEST_LOOPS. Times { NAME. Split(' ', 2).

First } #Testloops } #b. Report b. Report('regex') { TEST_LOOPS.

Times { NAME/^\S*/ } #Testloops } #b. Report b. Report('dollar backtick') { TEST_LOOPS.

Times { NAME =~ / / $` } #Testloops } #b. Report } #Benchmark Gives Rehearsal --------------------------------------------------- 0 30.453000 0.797000 31.250000 ( 31.311608) 02regex 21.094000 0.000000 21.094000 ( 23.651419) 02string 19.188000 0.000000 19.188000 ( 20.999215) first 34.187000 0.782000 34.969000 ( 39.935742) first2regex 24.078000 0.000000 24.078000 ( 26.813530) first2string 19.125000 0.000000 19.125000 ( 19.411310) regex 13.094000 0.000000 13.094000 ( 13.242792) dollar backtick 12.219000 0.000000 12.219000 ( 12.227719) ---------------------------------------- total: 175.017000sec user system total real 0 30.859000 0.734000 31.593000 ( 33.809723) 02regex 20.891000 0.000000 20.891000 ( 21.156553) 02string 18.890000 0.000000 18.890000 ( 19.997051) first 32.516000 0.812000 33.328000 ( 36.216360) first2regex 22.000000 0.000000 22.000000 ( 22.853772) first2string 19.781000 0.000000 19.781000 ( 22.010805) regex 13.359000 0.000000 13.359000 ( 14.892417) dollar backtick 12.328000 0.000000 12.328000 ( 13.253315).

I was curious, what's the fastest solution. My result was the regex of Wayne. One word about split: If your name has more parts, you may stop after the first split.

You may do this with String#split(/\s/, 2) My Benchmark: require 'benchmark' TEST_LOOPS = 10_000_000 NAME = 'Jon Doe the third' #~ p NAME. Split0 #~ p NAME.split. First #~ p NAME/^\S*/ #~ p NAME.

Split(/\s/, 2). First #~ p NAME. Split(/\s/, 2)0 #~ p NAME.

Split(' ', 2)0 #~ exit Benchmark. Bmbm(10) {|b| b. Report('0') { TEST_LOOPS.

Times { NAME. Split0 } #Testloops } #b. Report b.

Report('02regex') { TEST_LOOPS. Times { NAME. Split(/\s/, 2)0 } #Testloops } #b.

Report b. Report('02string') { TEST_LOOPS. Times { NAME.

Split(' ', 2)0 } #Testloops } #b. Report b. Report('first') { TEST_LOOPS.

Times { NAME.split. First } #Testloops } #b. Report b.

Report('first2regex') { TEST_LOOPS. Times { NAME. Split(/\s/, 2).

First } #Testloops } #b. Report b. Report('first2string') { TEST_LOOPS.

Times { NAME. Split(' ', 2). First } #Testloops } #b.

Report b. Report('regex') { TEST_LOOPS. Times { NAME/^\S*/ } #Testloops } #b.

Report b. Report('dollar backtick') { TEST_LOOPS. Times { NAME =~ / / $` } #Testloops } #b.

Report } #Benchmark The Result: Rehearsal --------------------------------------------------- 0 30.453000 0.797000 31.250000 ( 31.311608) 02regex 21.094000 0.000000 21.094000 ( 23.651419) 02string 19.188000 0.000000 19.188000 ( 20.999215) first 34.187000 0.782000 34.969000 ( 39.935742) first2regex 24.078000 0.000000 24.078000 ( 26.813530) first2string 19.125000 0.000000 19.125000 ( 19.411310) regex 13.094000 0.000000 13.094000 ( 13.242792) dollar backtick 12.219000 0.000000 12.219000 ( 12.227719) ---------------------------------------- total: 175.017000sec user system total real 0 30.859000 0.734000 31.593000 ( 33.809723) 02regex 20.891000 0.000000 20.891000 ( 21.156553) 02string 18.890000 0.000000 18.890000 ( 19.997051) first 32.516000 0.812000 33.328000 ( 36.216360) first2regex 22.000000 0.000000 22.000000 ( 22.853772) first2string 19.781000 0.000000 19.781000 ( 22.010805) regex 13.359000 0.000000 13.359000 ( 14.892417) dollar backtick 12.328000 0.000000 12.328000 ( 13.253315).

– Andrew Grimm Jul 3 at 22:55 It's ok, just do it. – knut Jul 4 at 6:35.

Extract_scope(point)RegionReturns the extent of the syntax name assigned to the character at the given point. Scope_name(point)StringReturns the syntax name assigned to the character at the given point. Score_selector(point, selector)IntMatches the selector against the scope at the given location, returning a score.

A score of 0 means no match, above 0 means a match.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions