AS3修剪逗号分隔的字符串(例如,由用户输入的逗号分隔的关键字列表)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3修剪逗号分隔的字符串(例如,由用户输入的逗号分隔的关键字列表)相关的知识,希望对你有一定的参考价值。
This function trims a string and removes empty entries in a comma delimited list, removes extra commas from the beginning and end and also remove extra space characters between words.
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
以上是关于AS3修剪逗号分隔的字符串(例如,由用户输入的逗号分隔的关键字列表)的主要内容,如果未能解决你的问题,请参考以下文章
powershell 分割逗号分隔字符串和使用PowerShell修剪项目的三种不同方法