简体   繁体   中英

How to Split a variable in javascript

I am in a project where I need to split a JavaScript variable into individual values.

My variable looks like this :

["ch  au"] or ["ch"]or ["au"]

I want to check whether ch or au is present in that variable. If present, it needs to be saved to another variable.

How to check wheather ch and au are present in a specific variable.

var countryArray = str.split(" ");

This will split your string into an array. In this case your delimiter seems to be whitespace.

I'm not sure exactly what you want, but here are some pointers. :)

var myString = 'ch  au';

// Check if string contains string
if (myString.indexOf('ch') >= 0) {

// Split string (in this case by '  ')
var myArray = myString.split('  ');
// This returns an array that would look like: ['ch', 'au']

// Check if array contains string
if (myArray.indexOf('ch') >= 0) {

// Take individual value from an array
var newString = myArray[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM