Check Each Character of a String?

M

MSUTech

Hello,

What is the best way to check each character within a string?

For doing something like encryption, where you check character 1 and replace
it with a different character.. then check character 2 and replace it with a
different character.... etc.... until completing the string?

thanks....
 
E

Evertjan.

=?Utf-8?B?TVNVVGVjaA==?= wrote on 11 jul 2005 in
microsoft.public.inetserver.asp.general:
What is the best way to check each character within a string?

For doing something like encryption, where you check character 1 and
replace it with a different character.. then check character 2 and
replace it with a different character.... etc.... until completing the
string?

vbscript?

myoutput=""

for i=1 to len(mystring)
myoutput = myoutput & checkReplace(mid(mystring,i,1))
next
 
B

Bob Barrows [MVP]

MSUTech said:
Hello,

What is the best way to check each character within a string?

For doing something like encryption, where you check character 1 and
replace it with a different character.. then check character 2 and
replace it with a different character.... etc.... until completing
the string?

thanks....

The best way is to use the replace() function.

Get the vbscript documentation here: http://tinyurl.com/7rk6


Bob Barrows
 
E

Evertjan.

Bob Barrows [MVP] wrote on 12 jul 2005 in
microsoft.public.inetserver.asp.general:
The best way is to use the replace() function.

You cannot specify the position with replace()

say the first A needs to become a C
the second A a D
 
B

Bob Barrows [MVP]

Evertjan. said:
Bob Barrows [MVP] wrote on 12 jul 2005 in
microsoft.public.inetserver.asp.general:


You cannot specify the position with replace()

say the first A needs to become a C
the second A a D

Oh, is that what you think he meant? i wish people would learn how to ask
questions ....
 
M

MSUTech

My question is how do I look at each character within a string.... for
example...

there is a string "people"

what would be the best way to look at each character....

"p" .... and do some evaluation on the p ... THEN
"e" ... and do some evaluation on the e .... THEN
"o" .... and do some evaluation on the o .... etc....

of course... the string could be ANYTHING.... so, I am just trying to
determine the best way to do a character by character evaluation...

thanks....
 
B

Bob Barrows [MVP]

Looping through a string could not be simpler:

dim s, i, curchar
s="people"
for i = 1 to len(s)
curchar=mid(s,i,1)
'evaluate the character
select case curchar
case "p"
'do something
case "e"
'do something else
case "o"
'do something else
end select
next

My point is: depending on what you really want to do, looping through the
string may not be the best approach. For example, say you want to replace
all "p" with "u", and all "e" with "z". I think the best way would be to
use Replace:

<%
dim s
s="people"
s=replace(replace(s,"p","u"),"e","z")
response.write s
%>

If you need to do something different, then a loop may be needed. I can't
say for sure without knowing what you really want to do.

Bob Barrows
 
M

MSUTech

Thanks.... actually, I think you are correct..... REPLACE may be the best
thing to do.. because I will ALWAYS replace a certain character with another
certain character...

so, Thanks!
 
E

Evertjan.

=?Utf-8?B?TVNVVGVjaA==?= wrote on 12 jul 2005 in
microsoft.public.inetserver.asp.general:
I will ALWAYS replace a certain character
with another certain character...

In that case, this old and famous rot13 function could be interesting:

Function ROT13(myInput)
Dim txt
txt = ""
Dim character
Dim Position
Const coding =
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMabcdefghijklmnopqrstuvwxyzabcdefghi
jklm"

For i = 1 To Len(myInput)
character = Mid(myInput, i, 1)
position = InStr(coding, character)
If position > 0 Then character = Mid(coding, position + 13, 1)
txt = txt & character
Next
ROT13 = txt
End Function
 
D

Dave Anderson

MSUTech said:
Thanks.... actually, I think you are correct..... REPLACE
may be the best thing to do.. because I will ALWAYS replace
a certain character with another certain character...

If I may ask, why are you bothering to implement a simple substitution
cypher?



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top