ActionScript 3 AS3修剪逗号分隔字符串(例如,用户输入的逗号分隔关键字列表)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3修剪逗号分隔字符串(例如,用户输入的逗号分隔关键字列表)相关的知识,希望对你有一定的参考价值。
function removeExtraSpacesFromKeywords($keywords:String):String {
var trimmedKeywords:String;
var doubleCommaRegExp:RegExp = /,,/gi;
var doubleSpaceRegExp:RegExp = / /gi;
var keywordsArray:Array = $keywords.split(",");
for (var i=0; i<keywordsArray.length; i++) {
keywordsArray[i] = keywordsArray[i].replace(/^\s+|\s+$/g, '');
}
trimmedKeywords = keywordsArray.toString();
do {
trimmedKeywords = trimmedKeywords.replace(doubleCommaRegExp, ",");
} while (doubleCommaRegExp.test(trimmedKeywords));
do {
trimmedKeywords = trimmedKeywords.replace(doubleCommaRegExp, ",");
} while (doubleCommaRegExp.test(trimmedKeywords));
do {
trimmedKeywords = trimmedKeywords.replace(doubleCommaRegExp, ",");
} while (doubleCommaRegExp.test(trimmedKeywords));
var firstChar:String = trimmedKeywords.substr(0, 1);
if (firstChar == ",") {
trimmedKeywords = trimmedKeywords.substring(1, trimmedKeywords.length);
}
var lastChar:String = trimmedKeywords.substr(trimmedKeywords.length-1, 1);
if (lastChar == ",") {
trimmedKeywords = trimmedKeywords.substring(0, trimmedKeywords.length-1);
}
do {
trimmedKeywords = trimmedKeywords.replace(doubleSpaceRegExp, " ");
} while (doubleSpaceRegExp.test(trimmedKeywords));
do {
trimmedKeywords = trimmedKeywords.replace(doubleSpaceRegExp, " ");
} while (doubleSpaceRegExp.test(trimmedKeywords));
return trimmedKeywords;
}
var keywords:String = ",,,, ,, , one ,,, ,,, , , ,, ,,,, , ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, , , , , ,,,,,,,,,,,,,,,,,,,, ,,, , two , three , hello world , ,, ,,, ,,,,";
trace(keywords);
keywords = removeExtraSpacesFromKeywords(keywords);
trace(keywords);
// OUTPUT
// ,,,, ,, , one ,,, ,,, , , ,, ,,,, , ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, , , , , ,,,,,,,,,,,,,,,,,,,, ,,, , two , three , hello world , ,, ,,, ,,,,
// one,two,three,hello world
以上是关于ActionScript 3 AS3修剪逗号分隔字符串(例如,用户输入的逗号分隔关键字列表)的主要内容,如果未能解决你的问题,请参考以下文章
ActionScript 3 AS3 - 修剪特定宽度和高度的文本
ActionScript 3 AS3从字符串的开头和结尾修剪空格
使用 actionscript 3 修剪字符串
ActionScript 3 格式化数字逗号分隔符
如何在 Oracle 中修剪逗号分隔值中的前导零?
powershell 分割逗号分隔字符串和使用PowerShell修剪项目的三种不同方法