By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Check Each Character of a String?

P: n/a
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....
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
=?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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #2

P: n/a
MSUTech wrote:
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

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #3

P: n/a
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
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #4

P: n/a
Evertjan. wrote:
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


Oh, is that what you think he meant? i wish people would learn how to ask
questions ....
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #5

P: n/a
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....

"Bob Barrows [MVP]" wrote:
Evertjan. wrote:
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


Oh, is that what you think he meant? i wish people would learn how to ask
questions ....
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Jul 22 '05 #6

P: n/a
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

MSUTech wrote:
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....


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 22 '05 #7

P: n/a
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!

"Bob Barrows [MVP]" wrote:
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

MSUTech wrote:
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....


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Jul 22 '05 #8

P: n/a
=?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 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMabcdefghij klmnopqrstuvwxyzabcdefghi
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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #9

P: n/a
MSUTech wrote:
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.
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.