Check Each Character of a String?

Discussion in 'ASP General' started by MSUTech, Jul 11, 2005.

  1. MSUTech

    MSUTech Guest

    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....
     
    MSUTech, Jul 11, 2005
    #1
    1. Advertising

  2. MSUTech

    Evertjan. Guest

    =?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)
     
    Evertjan., Jul 11, 2005
    #2
    1. Advertising

  3. 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"
     
    Bob Barrows [MVP], Jul 11, 2005
    #3
  4. MSUTech

    Evertjan. Guest

    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)
     
    Evertjan., Jul 11, 2005
    #4
  5. 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"
     
    Bob Barrows [MVP], Jul 11, 2005
    #5
  6. MSUTech

    MSUTech Guest

    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"
    >
    >
    >
     
    MSUTech, Jul 12, 2005
    #6
  7. 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.
     
    Bob Barrows [MVP], Jul 12, 2005
    #7
  8. MSUTech

    MSUTech Guest

    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.
    >
    >
    >
     
    MSUTech, Jul 12, 2005
    #8
  9. MSUTech

    Evertjan. Guest

    =?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

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
     
    Evertjan., Jul 12, 2005
    #9
  10. 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.
     
    Dave Anderson, Jul 12, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    780
    those who know me have no need of my name
    May 28, 2006
  2. Replies:
    5
    Views:
    984
    X-Centric
    Jun 30, 2005
  3. Replies:
    3
    Views:
    517
    James Kanze
    Nov 18, 2007
  4. Victor
    Replies:
    3
    Views:
    148
    Victor
    Sep 12, 2003
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    820
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page