迭代字符串中的每个字母 [英] iterating each letter in string

查看:66
本文介绍了迭代字符串中的每个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串。我想把这个字符串

并使每个字符更改,直到它到达另一个

字符串的相同位置。


示例:


原始字符串:"纽约"

最终字符串:SAN FRANSISCO


我希望每个字符都能在计时器上循环显示字符
并显示字母表中的每个字符,直到它与起始字符的位置相同的

字符

所以原始字符串中的第一个字符串,这是一个空格,

会循环显示所有字符,直到它达到S


这应该看起来像那些翻滚出发/抵达

当信息发生变化时在机场签到。


任何人???


如果你能'这样做,然后你可以填写我如何去做吗?

甚至可能是一些伪代码?

解决方案

Mr.Clean写道:

当信息发生变化时,这应该看起来像机场中的翻转出发/到达
标志。




完成之前,这里是:


函数fliptext(oElem,sDest,nTime){

if(

!oElem

| | "串QUOT; != typeof oElem.value

|| "串QUOT; != typeof sDest

)返回null;

if(!nTime || isNaN(nTime))nTime = 100;

if(oElem .timer)window.clearTimeout(oElem.timer);

var sCurr = oElem.value;

while(sCurr.length< sDest.length)sCurr + =" ; " ;;

while(sDest.length< sCurr.length)sDest + =" " ;;

oElem.value = sCurr;

oElem.dest = sDest;

oElem.timer = window.setInterval(function() {

var c1,c2,i,sNew ="",

sCurr = oElem.value,sDest = oElem.dest;

for(i = 0; i< sCurr.length; i ++){

c1 = sCurr.charCodeAt(i);

c2 = sDest.charCodeAt(i);

sNew + = String.fromCharCode(c2> c1?++ c1:c2< c1? - c1:c1);

}

oElem.value = sNew;

if(sNew == sDest){

window.clearTimeout(oElem.timer);

oElem.timer = null;

}

},nTime);

}

[...]

fliptext(

document.forms [''foo'']。elements [''bar''],

''旧金山'',



);

[...]


fliptext(

document.forms [''foo'']。elements [''bar''],

''NEW YORK'',

100

);

[... ]

< form name =" foo" [...]>

< input name =" bar" style =" font-family:monospace; border:0">

[...]


ciao,dhgm


在文章< 36 ************* @ individual.net>中,使用了反复数据@ innoline-

systemtechnik.de说。 ..

Mr.Clean写道:

当信息发生变化时,这应该看起来像机场中的翻转出发/到达
标志完成之前,这里是:




如何限制它翻转的字符并设置长度
结果/目的地字符串


" ABCDEFGHIJKLMNOPQRSTUVWXYZ .-"


Mr.Clean写道:

如何限制它翻转的字符


一些变化,快速入侵并且大部分未经测试:


函数fliptext(oElem,sDest,nTime){

var i,c,s,sDispChar s =" ABCDEFGHIJKLMNOPQRSTUVWXYZ - 。" ;;

if(!oElem ||" string"!= typeof oElem.value)返回null;

if(oElem.timer)window。 clearTimeout(oElem.timer);

var sCurr = oElem.value.toUpperCase();

sDest = sDest.toUpperCase();

while(sCurr.length< sDest.length)sCurr + =" " ;;

while(sDest.length< sCurr.length)sDest + =" " ;;

sCurr = sCurr.toUpperCase();

for(i = 0,s ="" ;; i< sCurr.length; i ++)

s + = sDispChars.indexOf(c = sCurr.charAt(i))> -1? c: " ;;

oElem.value = s.toUpperCase();

for(i = 0,s ="" ;; i< sDest.length; i ++)

s + = sDispChars.indexOf(c = sDest.charAt(i))> -1? c: " ;;

oElem.dest = s.toUpperCase();

oElem.timer = window.setInterval(function(){

var c1,c2,i,sNew ="",

sCurr = oElem.value,sDest = oElem.dest;

for(i = 0; i< sCurr .length; i ++){

c1 = sDispChars.indexOf(sCurr.charAt(i));

c2 = sDispChars.indexOf(sDest.charAt(i));

sNew + = sDispChars.charAt(c2> c1?++ c1:c2< c1? - c1:c1);

}

oElem.value = sNew;

if(sNew == sDest){

window.clearTimeout(oElem.timer);

oElem.timer = null;

}

},nTime);

}

并设置长度结果/目标字符串?




使用的长度是两个字符串的最大长度

sDest和sCurr。要获得包含更多字符的输出,只需

为第二个参数sDest添加一些空格。


ciao,dhgm


I have a string. I''d like to take this string
and make each char change until it gets to another
char at the same position of the string.

Example:

Original String: " NEW YORK "
Final String: "SAN FRANSISCO"

I''d like each char to loop through the chars on a timer
and show each char in the alphabet until it gets to the
char in the same position as the beginning char
So the first char in the original string, which is a space,
would loop through and show all chars until it reaches "S"

This is supposed to look like those rollover departure/arrival
signs in an airport when the info changes.

Anyone???

If you can''t do this, then can you fill me in on HOW to do it?
Maybe even some pseudocode?

解决方案

Mr.Clean wrote:

This is supposed to look like those rollover departure/arrival
signs in an airport when the info changes.



Done that before, here it is:

function fliptext(oElem, sDest, nTime) {
if (
!oElem
|| "string" != typeof oElem.value
|| "string" != typeof sDest
) return null;
if (!nTime || isNaN(nTime)) nTime = 100;
if (oElem.timer) window.clearTimeout(oElem.timer);
var sCurr = oElem.value;
while (sCurr.length < sDest.length) sCurr += " ";
while (sDest.length < sCurr.length) sDest += " ";
oElem.value = sCurr;
oElem.dest = sDest;
oElem.timer = window.setInterval(function() {
var c1, c2, i, sNew = "",
sCurr = oElem.value, sDest = oElem.dest;
for (i=0; i<sCurr.length; i++) {
c1 = sCurr.charCodeAt(i);
c2 = sDest.charCodeAt(i);
sNew += String.fromCharCode(c2 > c1? ++c1 : c2 < c1? --c1 : c1);
}
oElem.value = sNew;
if (sNew == sDest) {
window.clearTimeout(oElem.timer);
oElem.timer = null;
}
}, nTime);
}
[...]
fliptext(
document.forms[''foo''].elements[''bar''],
''SAN FRANCISCO'',
100
);
[...]

fliptext(
document.forms[''foo''].elements[''bar''],
'' NEW YORK'',
100
);
[...]
<form name="foo" [...]>
<input name="bar" style="font-family:monospace;border:0">
[...]

ciao, dhgm


In article <36*************@individual.net>, usereplytoinstead@innoline-
systemtechnik.de says...

Mr.Clean wrote:

This is supposed to look like those rollover departure/arrival
signs in an airport when the info changes.



Done that before, here it is:



How can I limit the chars that it flips through and set the length
of the result/destination string?

"ABCDEFGHIJKLMNOPQRSTUVWXYZ .-"


Mr.Clean wrote:

How can I limit the chars that it flips through
Some changes, quick hacked and mostly untested:

function fliptext(oElem, sDest, nTime) {
var i, c, s, sDispChars = " ABCDEFGHIJKLMNOPQRSTUVWXYZ-.";
if (!oElem || "string" != typeof oElem.value) return null;
if (oElem.timer) window.clearTimeout(oElem.timer);
var sCurr = oElem.value.toUpperCase();
sDest = sDest.toUpperCase();
while (sCurr.length < sDest.length) sCurr += " ";
while (sDest.length < sCurr.length) sDest += " ";
sCurr = sCurr.toUpperCase();
for (i=0, s=""; i<sCurr.length; i++)
s += sDispChars.indexOf(c = sCurr.charAt(i)) > -1? c : " ";
oElem.value = s.toUpperCase();
for (i=0, s=""; i<sDest.length; i++)
s += sDispChars.indexOf(c = sDest.charAt(i)) > -1? c : " ";
oElem.dest = s.toUpperCase();
oElem.timer = window.setInterval(function() {
var c1, c2, i, sNew = "",
sCurr = oElem.value, sDest = oElem.dest;
for (i=0; i<sCurr.length; i++) {
c1 = sDispChars.indexOf(sCurr.charAt(i));
c2 = sDispChars.indexOf(sDest.charAt(i));
sNew += sDispChars.charAt(c2 > c1? ++c1 : c2 < c1? --c1 : c1);
}
oElem.value = sNew;
if (sNew == sDest) {
window.clearTimeout(oElem.timer);
oElem.timer = null;
}
}, nTime);
}
and set the length of the result/destination string?



The lenght used is the maximum of the lengths of the two strings
sDest and sCurr. To get an output with more characters, simply
add some spaces to the second argument, sDest.

ciao, dhgm


这篇关于迭代字符串中的每个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆