日期:2014-05-17  浏览次数:20878 次

求返回汉字和字母混合字符串中最大字符组合的函数
例如有这样一个字符串:
MaxChars( "中abc华bc人a民共abcde和国 ")= "abcde "

自己写了几个函数,但都不理想。求其他算法。

------解决方案--------------------
<script>

function MaxChars(str)
{
str=str.replace(/[^a-z]/g, ", ");
a=str.split( ", ");
var result=new Array();
for(var i=0;i <a.length;i++)
{
result[i]=a[i].length;
}
var r=result.sort(function(a,b)
{if(a> b)
{
return -1;
}
else
{
return 1;
}
});

for(var i=0;i <a.length;i++)
{
if(a[i].length==r[0])
{
document.write(a[i]);
}
}

}
MaxChars( "中abcsss华bc人a民hhhhhhhhhhhhhhhhhhhhh共abcde和国ffffffffff ");
</script>
------解决方案--------------------
如果是要提最大E文字串的话
function GetMaxEnglish(strIn)
Dim i,strT,strP
For i=1 To Len(strIn)
If Asc(Mid(strIn,i,1)) <0 Then
If Len(strT)> Len(strP) Then strP=strT
strT= " "
Else
strT=strT & Mid(strIn,i,1)
End If
Next

If Len(strT)> Len(strP) Then strP=strT
GetMaxEnglish=strP
End function
------解决方案--------------------
还可以用正则
function GetMaxEnglish(strIn)
Dim oMatches,oRe,i,j
Set oRe=new Regexp
oRe.Global=True
oRe.IgnoreCase=True
oRe.Pattern= "[a-z]* "
Set oMatches=oRe.Execute(strIn)
For i=0 To oMatches.Count-1
If Len(oMatches(i).value) > Len(oMatches(j).value) Then j=i
If i=oMatches.Count-1 Then GetMaxEnglish=oMatches(j).value
Next
End function
------解决方案--------------------
不能输入全角字符不就好了嘛