如果字符串恰好包含 2 个 space,则先删除 space
Remove first space if string contains exactly 2 spaces
我在尝试删除字符串的第一个 space 时遇到问题,如果该字符串中有 2 个 space。例如它应该把 "Fully Functional Method" 变成 "FullyFunctional Method",但是 "Functional Method" 不应该改变,因为它只有 1 个 space。如果字符串包含 2 spaces.
,我真的想不出先删除 space 的方法
对字符串进行线性扫描。统计space的个数,如果有,记录第一个space的索引。如果有两个 space,则 return 一个字符串,它是不包括第一个 space 的字符和第一个 space 之后的字符的串联。
保持简单。可以使用正则表达式解决您的问题,但请记住,在未排序的集合中查找特定字符的最坏情况时间复杂度始终为 O(N),因此它不会更快。
我不知道你到底想做什么,但你可以搜索 RegExp and String.replace() 来替换字符串中的一些东西。
这里还有一个link来理解Characters, metacharacters, and metasequences.
var myPattern1:RegExp = / /g;
var str1:String = "This is a string that contains double spaces.";
trace(str1.replace(myPattern1, " "));
//this replaces all " " by " "...
//outputs : This is a string that contains double spaces.
或者在你的情况下(我想)是这样的
var myPattern2:RegExp = / /;
var str2:String = "Fully Functional Method";
trace(str2.replace(myPattern2, ""));
//If you omit the g, only the first space will be replaced by ""
//outputs : FullyFunctional Method
使用 RegExp 可以做很多事情,我不会在这里解释......
只需在 Adobe 网站上查看...
这是处理字符串的一种快速有效的方法。
我希望这将有所帮助。
由于您检查了那些 link,您会明白我的示例是纯粹的粗略示例,应该修改为具有 FullyFunctional 方法。 :D
我在尝试删除字符串的第一个 space 时遇到问题,如果该字符串中有 2 个 space。例如它应该把 "Fully Functional Method" 变成 "FullyFunctional Method",但是 "Functional Method" 不应该改变,因为它只有 1 个 space。如果字符串包含 2 spaces.
,我真的想不出先删除 space 的方法对字符串进行线性扫描。统计space的个数,如果有,记录第一个space的索引。如果有两个 space,则 return 一个字符串,它是不包括第一个 space 的字符和第一个 space 之后的字符的串联。
保持简单。可以使用正则表达式解决您的问题,但请记住,在未排序的集合中查找特定字符的最坏情况时间复杂度始终为 O(N),因此它不会更快。
我不知道你到底想做什么,但你可以搜索 RegExp and String.replace() 来替换字符串中的一些东西。 这里还有一个link来理解Characters, metacharacters, and metasequences.
var myPattern1:RegExp = / /g;
var str1:String = "This is a string that contains double spaces.";
trace(str1.replace(myPattern1, " "));
//this replaces all " " by " "...
//outputs : This is a string that contains double spaces.
或者在你的情况下(我想)是这样的
var myPattern2:RegExp = / /;
var str2:String = "Fully Functional Method";
trace(str2.replace(myPattern2, ""));
//If you omit the g, only the first space will be replaced by ""
//outputs : FullyFunctional Method
使用 RegExp 可以做很多事情,我不会在这里解释...... 只需在 Adobe 网站上查看... 这是处理字符串的一种快速有效的方法。 我希望这将有所帮助。 由于您检查了那些 link,您会明白我的示例是纯粹的粗略示例,应该修改为具有 FullyFunctional 方法。 :D