How to split string into paragraphs using first comma?

前端 未结 4 472
谎友^
谎友^ 2021-02-05 01:30

I have string: @address = \"10 Madison Avenue, New York, NY - (212) 538-1884\" What\'s the best way to split it like this?

10 Madison Avenu

4条回答
  •  难免孤独
    2021-02-05 02:16

    Even though @address.split(",",2) is correct. Running a benchmark for split, partition and a regex solution such as @adress.match(/^([^,]+),\s*(.+)/) showed that partition is a bit better than split.

    On a 2,6 GHz Intel Core i5, 16 GB RAM computer and 100_000 runs: user system total real partition 0.690000 0.000000 0.690000 ( 0.697015) regex 1.910000 0.000000 1.910000 ( 1.908033) split 0.780000 0.010000 0.790000 ( 0.788240)

提交回复
热议问题