1

我想根据split一个字符串值<br/>

示例字符串:

""* Minimum 3 digits allowed<br/>" * Maximum 9 digits & 2 precisions allowed<br/>"

在上述情况下,数组中将有两个值

1. * Minimum 3 digits allowed
2. * Maximum 9 digits & 2 precisions allowed

并且需要从数组中找到字符串的长度(已经拆分),它在数组中具有最大长度。

所以答案将是 40

4

3 回答 3

1
var str = '* Minimum 3 digits allowed<br/>* Maximum 9 digits & 2 precisions allowed<br/>';

var spltArr = str.split("<br/>");
var longStr = null;
var maxLen = -1;
for(var i = 0; i < spltArr.length; i++ ){
    if( longStr === null ){
        longStr =  spltArr[i];
        maxLen =  spltArr[i].length;  
    }
    if( spltArr[i].length > maxLen ){
        longStr =  spltArr[i];
        maxLen =  spltArr[i].length;
    }
}
console.log("Max len: ", maxLen, "String:", longStr);

小提琴

于 2012-09-13T10:04:02.850 回答
1
var str = "* Minimum 3 digits allowed<br/> * Maximum 9 digits & 2 precisions allowed<br/>";
var arr = str.split('<br/>');

var longest = 0;
var longest_string = '';

for(var i = 0; i < arr.length; i++) {
    if(arr[i].length > longest) {
        longest = arr[i].length;
        longest_string = arr[i];
    }
}

alert("Longest string is: " + longest_string + " (char count: " + longest + ")");
于 2012-09-13T10:00:03.330 回答
1
var str = "* Minimum 3 digits allowed<br/>* Maximum 9 digits & 2 precisions allowed<br/>";
var lines = str.split('<br/>');
var longest = '';

for(i=0; i<lines.length; i++) {
    if(lines[i].length > longest.length) {
        longest = lines[i];
    }
}
alert('Longest string:' + longest + ' (Length: ' + longest.length + ')');
于 2012-09-13T10:09:08.863 回答