How do I split a string, breaking at a particular character?

后端 未结 17 2389
误落风尘
误落风尘 2020-11-21 05:07

I have this string

\'john smith~123 Street~Apt 4~New York~NY~12345\'

Using JavaScript, what is the fastest way to parse this into



        
17条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 05:43

    split() method in javascript is used to convert a string to an array. It takes one optional argument, as a character, on which to split. In your case (~).

    If splitOn is skipped, it will simply put string as it is on 0th position of an array.

    If splitOn is just a “”, then it will convert array of single characters.

    So in your case

    var arr = input.split('~');

    you will get name at arr[0] and street at arr1.

    You can read for more detail explanation at Split on in JavaScript

提交回复
热议问题