最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - What is the best way to do split() in javascript and ignore blank entries? - Stack Overflow

programmeradmin3浏览0评论

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
Add a comment  | 

6 Answers 6

Reset to default 10

You 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_]

发布评论

评论列表(0)

  1. 暂无评论