I have the following C# code that i need to convert to javascript:
static private string[] ParseSemicolon(string fullString)
{
if (String.IsNullOrEmpty(fullString))
return new string[] { };
if (fullString.IndexOf(';') > -1)
{
return fullString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(str => str.Trim()).ToArray();
}
else
{
return new[] { fullString.Trim() };
}
}
i see that javascript has a split() function as well but i wanted to see if there is built in support for the other checks or i have to do an additional loop around the array afterwards to "clean up" the data?
I have the following C# code that i need to convert to javascript:
static private string[] ParseSemicolon(string fullString)
{
if (String.IsNullOrEmpty(fullString))
return new string[] { };
if (fullString.IndexOf(';') > -1)
{
return fullString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(str => str.Trim()).ToArray();
}
else
{
return new[] { fullString.Trim() };
}
}
i see that javascript has a split() function as well but i wanted to see if there is built in support for the other checks or i have to do an additional loop around the array afterwards to "clean up" the data?
Share Improve this question asked Mar 17, 2014 at 11:46 leoraleora 196k367 gold badges906 silver badges1.4k bronze badges 3- maybe you can replace ";;" with ";" in the string beforehand? – user2571870 Commented Mar 17, 2014 at 11:55
- @Wilmer what about ;;; or ;;;; etc – Aamir Afridi Commented Mar 17, 2014 at 11:56
- would have to be in a loop – user2571870 Commented Mar 17, 2014 at 12:00
6 Answers
Reset to default 10You can use filter, but this function is implemented only in more recent browsers.
"dog;in;bin;;cats".split(";").filter(function (x) { return x != ""; });
You can use a RegExp with a quantifier to split on any consecutive count of the delimiter:
var parts = input.split(/;+/);
Such as:
var input = "foo;;;bar;;;;;;;;;baz";
var parts = input.split(/;+/);
console.log(parts);
// [ "foo", "bar", "baz" ]
Try this
"hello;aamir;;afridi".split(';').filter(Boolean)
or
"hello;;aamir;;;;afridi".split(';').filter(Boolean)
results in
["hello", "aamir", "afridi"]
And to convert it back to string, use this
"hello;aamir;;afridi".split(';').filter(Boolean).join(' ')
or
"hello;aamir;;afridi".split(';').filter(Boolean).join(';')
Unfortunately javascript split() does not check for doublespaces etc, you would need to clean up your array later
var str = "How;are;;you;my;;friend?";
var arr = str.split(";");
alert(arr);
http://jsfiddle.net/p58qm/2/
You can then update the array with this loop
len = arr.length, i;
for(i = 0; i < len; i++ )
arr[i] && arr.push(arr[i]); // copy non-empty values to the end of the array
arr.splice(0 , len);
alert(arr);
http://jsfiddle.net/p58qm/3/
Try this:
function parseSemicolon(fullString) {
var result = new Array();
if (!!fullString && fullString.length > 0) {
fullString = fullString.trim();
if (fullString.indexOf(';') > -1) {
result = fullString.split(';');
for (var i = 0; i < result.length; i++) {
result[i] = result[i].trim();
}
} else {
result = new Array(fullString);
}
}
return result;
}
This should have exactly the same effect as your C# script.
Split also requires handling the empty results. However, where's another way
http://jsfiddle.net/9mHug/1/
var str = "aaa,bbb;ccc ddd , eee";
var sp = str.match(/\w+/g);
alert(sp);
This will give you a clean array of only the words.
\w
is a regex search that will find all letters of certain type [a-zA-Z0-9_]