How to replace first and last "," in a string in asp vbscript?

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default How to replace first and last "," in a string in asp vbscript?

    How can I replace first as well as last "," in a string?

    Mystring = ",3,2,5,6,1,65,22,"

    I would like
    CorrectedString = "3,2,5,6,1,65,22"

    I would like to replace commas on each sides with "".
    I am new to asp and would be grateful for your help.
    Thanks in advance.
    Learning Guest

  2. Similar Questions and Discussions

    1. "Error Creating Control" and "Cast from String"
      I'm creating a custom date control. In appearance, it's just a textbox and a button. It has three custom properties: CalDate, CalDateType and...
    2. "replace pages" / "rotate pages" orientation issue
      in Adobe acrobat you can open a PDF file and "replace pages" (some or all) with pages from another PDF file. it used to be that when I did this,...
    3. #26292 [Opn->Bgs]: substr returns "0" for any offset on the string "0"
      ID: 26292 Updated by: sniper@php.net Reported By: ravacholp at hotmail dot com -Status: Open +Status: ...
    4. #25763 [NEW]: Why the string "1.10" is equal to the string "1.1"?
      From: jparneodo at yahoo dot fr Operating system: RH7.2 PHP version: 4.3.3 PHP Bug Type: Strings related Bug description: ...
    5. convert visual basic "string" data type to DB2 "blob"
      Does anyone know if a visual basic string data type can be converted to DB2 blob datatype? I have all data in XML files and I use Visual Basic to...
  3. #2

    Default Re: How to replace first and last "," in a string in asp vbscript?

    InStr()

    -and-

    InStrRev()

    The first can give you the first ",", and the latter will do the same, but
    starting from the end of the string. You can get the VBScript documentation
    from here:


    [url]http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en[/url]

    Cheers
    Ken


    "Learning" <question_asp@yahoo.com> wrote in message
    news:99a92f8.0307151244.1beebfb0@posting.google.co m...
    : How can I replace first as well as last "," in a string?
    :
    : Mystring = ",3,2,5,6,1,65,22,"
    :
    : I would like to replace commas on each sides with "".
    : I am new to asp and would be grateful for your help.
    : Thanks in advance.


    Ken Schaefer Guest

  4. #3

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Please post in one group only. Answered in:
    microsoft.public.inetserver.asp.general

    Cheers
    Ken

    "Learning" <question_asp@yahoo.com> wrote in message
    news:99a92f8.0307151247.4e876cef@posting.google.co m...
    : How can I replace first as well as last "," in a string?
    :
    : Mystring = ",3,2,5,6,1,65,22,"
    :
    : I would like
    : CorrectedString = "3,2,5,6,1,65,22"
    :
    : I would like to replace commas on each sides with "".
    : I am new to asp and would be grateful for your help.
    : Thanks in advance.


    Ken Schaefer Guest

  5. #4

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Mystring = Mid(MyString, 2, Len(Mystring)-2)

    But let me ask, where is this string coming from?

    Ray at work

    "Learning" <question_asp@yahoo.com> wrote in message
    news:99a92f8.0307151244.1beebfb0@posting.google.co m...
    > How can I replace first as well as last "," in a string?
    >
    > Mystring = ",3,2,5,6,1,65,22,"
    >
    > I would like to replace commas on each sides with "".
    > I am new to asp and would be grateful for your help.
    > Thanks in advance.

    Ray at Guest

  6. #5

    Default How to replace first and last "," in a string in asp vbscript?

    Mystring = Left(Mystring,Len(Mystring) - 1)
    Mystring = Mid(Mystring,2)

    That should do it.
    >-----Original Message-----
    >How can I replace first as well as last "," in a string?
    >
    >Mystring = ",3,2,5,6,1,65,22,"
    >
    >I would like to replace commas on each sides with "".
    >I am new to asp and would be grateful for your help.
    >Thanks in advance.
    >.
    >
    MDW Guest

  7. #6

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Ray, I believe it should be
    Len(Mystring) - 1
    not
    Len(Mystring) - 2


    "Ray at <%=sLocation%>" <ask@me.forit> wrote in message
    news:eS5XvVxSDHA.2276@TK2MSFTNGP10.phx.gbl...
    > Mystring = Mid(MyString, 2, Len(Mystring)-2)
    >
    > But let me ask, where is this string coming from?
    >
    > Ray at work
    >
    > "Learning" <question_asp@yahoo.com> wrote in message
    > news:99a92f8.0307151244.1beebfb0@posting.google.co m...
    > > How can I replace first as well as last "," in a string?
    > >
    > > Mystring = ",3,2,5,6,1,65,22,"
    > >
    > > I would like to replace commas on each sides with "".
    > > I am new to asp and would be grateful for your help.
    > > Thanks in advance.
    >
    >

    Michael Guest

  8. #7

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Also:

    MyString = Left(MyString,Len(MyString)-1)
    MyString = Right(MyString,Len(MyString)-1)


    "Learning" <question_asp@yahoo.com> wrote in message
    news:99a92f8.0307151244.1beebfb0@posting.google.co m...
    > How can I replace first as well as last "," in a string?
    >
    > Mystring = ",3,2,5,6,1,65,22,"
    >
    > I would like to replace commas on each sides with "".
    > I am new to asp and would be grateful for your help.
    > Thanks in advance.

    wetchman Guest

  9. #8

    Default Re: How to replace first and last "," in a string in asp vbscript?

    All of these suggestions work only if the string is in the exact specified
    format.

    If the original poster wishes to find the first comma, and the last comma,
    and the string is in some arbitrary format, then you need InStr() and
    InStrRev()

    Cheers
    Ken

    "wetchman" <wetchman@NOSPAMhotmail.com> wrote in message
    news:gI%Qa.19628$J%2.1900465@news.alltel.net...
    : Also:
    :
    : MyString = Left(MyString,Len(MyString)-1)
    : MyString = Right(MyString,Len(MyString)-1)
    :
    :
    : "Learning" <question_asp@yahoo.com> wrote in message
    : news:99a92f8.0307151244.1beebfb0@posting.google.co m...
    : > How can I replace first as well as last "," in a string?
    : >
    : > Mystring = ",3,2,5,6,1,65,22,"
    : >
    : > I would like to replace commas on each sides with "".
    : > I am new to asp and would be grateful for your help.
    : > Thanks in advance.
    :
    :


    Ken Schaefer Guest

  10. #9

    Default Re: How to replace first and last "," in a string in asp vbscript?

    He wanted to get rid of the first and last comma though. So,

    ,a,b,c,d,e,f, is 13 characters long. If you Mid(), starting at the second
    character, you want 11 characters, which is 13-2. Just like if it's ",e,"
    he'd only want the e, which is 1 character which is len(",e,")-2.

    Ray at home


    "Michael" <a@b.c> wrote in message
    news:e6KazoxSDHA.1552@TK2MSFTNGP10.phx.gbl...
    > Ray, I believe it should be
    > Len(Mystring) - 1
    > not
    > Len(Mystring) - 2
    >
    >
    > "Ray at <%=sLocation%>" <ask@me.forit> wrote in message
    > news:eS5XvVxSDHA.2276@TK2MSFTNGP10.phx.gbl...
    > > Mystring = Mid(MyString, 2, Len(Mystring)-2)
    > >
    > > But let me ask, where is this string coming from?
    > >
    > > Ray at work
    > >
    > > "Learning" <question_asp@yahoo.com> wrote in message
    > > news:99a92f8.0307151244.1beebfb0@posting.google.co m...
    > > > How can I replace first as well as last "," in a string?
    > > >
    > > > Mystring = ",3,2,5,6,1,65,22,"
    > > >
    > > > I would like to replace commas on each sides with "".
    > > > I am new to asp and would be grateful for your help.
    > > > Thanks in advance.
    > >
    > >
    >
    >

    Ray at Guest

  11. #10

    Default Re: How to replace first and last "," in a string in asp vbscript?

    You're right. Mystring on the RHS still refers to the original Mystring.
    Somehow my brain went ahead, and I already subtracted one character from
    Mystring. Thanks.

    "Ray at <%=sLocation%>" <ray@ajf8jalskdfna.sefrhja7yasdf.com> wrote in
    message news:%236GSikzSDHA.1688@TK2MSFTNGP11.phx.gbl...
    > He wanted to get rid of the first and last comma though. So,
    >
    > ,a,b,c,d,e,f, is 13 characters long. If you Mid(), starting at the second
    > character, you want 11 characters, which is 13-2. Just like if it's ",e,"
    > he'd only want the e, which is 1 character which is len(",e,")-2.
    >
    > Ray at home
    >
    >
    > "Michael" <a@b.c> wrote in message
    > news:e6KazoxSDHA.1552@TK2MSFTNGP10.phx.gbl...
    > > Ray, I believe it should be
    > > Len(Mystring) - 1
    > > not
    > > Len(Mystring) - 2
    > >
    > >
    > > "Ray at <%=sLocation%>" <ask@me.forit> wrote in message
    > > news:eS5XvVxSDHA.2276@TK2MSFTNGP10.phx.gbl...
    > > > Mystring = Mid(MyString, 2, Len(Mystring)-2)
    > > >
    > > > But let me ask, where is this string coming from?
    > > >
    > > > Ray at work
    > > >
    > > > "Learning" <question_asp@yahoo.com> wrote in message
    > > > news:99a92f8.0307151244.1beebfb0@posting.google.co m...
    > > > > How can I replace first as well as last "," in a string?
    > > > >
    > > > > Mystring = ",3,2,5,6,1,65,22,"
    > > > >
    > > > > I would like to replace commas on each sides with "".
    > > > > I am new to asp and would be grateful for your help.
    > > > > Thanks in advance.
    > > >
    > > >
    > >
    > >
    >
    >

    Michael Guest

  12. #11

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Thanks for all your posts.
    Sorry I couldn't explain properly, Actually the string is coming from
    a loop so it can be lengthy or short. After reading your posts I think
    I need InStr()and InStrRev() functions.

    I know I can use this function to get the position of the first
    occurrence but what after that?

    Mystring = ",3,2,5,6,1,65,22,"
    Instr(MyString, ",") 'returns 1 for position 1

    I am new to asp and would really apreciate help from you guys.
    Thanks for all your help so far.
    Learning Guest

  13. #12

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Show the code for your loop. I'm 99% sure that we can drop the loop
    altogether and just give you a comma delimited string all in one swoop
    without needing to loop.

    Ray at work

    "Learning" <question_asp@yahoo.com> wrote in message
    news:99a92f8.0307161006.630063fa@posting.google.co m...
    > Thanks for all your posts.
    > Sorry I couldn't explain properly, Actually the string is coming from
    > a loop so it can be lengthy or short. After reading your posts I think
    > I need InStr()and InStrRev() functions.
    >
    > I know I can use this function to get the position of the first
    > occurrence but what after that?
    >
    > Mystring = ",3,2,5,6,1,65,22,"
    > Instr(MyString, ",") 'returns 1 for position 1
    >
    > I am new to asp and would really apreciate help from you guys.
    > Thanks for all your help so far.

    Ray at Guest

  14. #13

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Please go and download the documentation that I provided, and you will have
    the answer. InStr() has extra parameters that let you decide where to start
    the search.

    Cheers
    Ken

    "Learning" <question_asp@yahoo.com> wrote in message
    news:99a92f8.0307161006.630063fa@posting.google.co m...
    : Thanks for all your posts.
    : Sorry I couldn't explain properly, Actually the string is coming from
    : a loop so it can be lengthy or short. After reading your posts I think
    : I need InStr()and InStrRev() functions.
    :
    : I know I can use this function to get the position of the first
    : occurrence but what after that?
    :
    : Mystring = ",3,2,5,6,1,65,22,"
    : Instr(MyString, ",") 'returns 1 for position 1
    :
    : I am new to asp and would really apreciate help from you guys.
    : Thanks for all your help so far.


    Ken Schaefer Guest

  15. #14

    Default Re: How to replace first and last "," in a string in asp vbscript?

    I have downloaded the vb documents. Now I know that I can use Instrv()
    and InstrvRev() to get the positions.

    MyString = ",1,2,3,4,5,6,7,8,"
    MyString = Instr(MyString, ",") ' Position 1
    MyString = InstrRev(MyString, ",") ' Position 17

    But after getting the positions how do I remove the "," ?
    Do I need another function to remove them?
    I am confused.
    That would be great if you can help.

    Here is the code.
    '----------------------------------------------------------
    Dim rsNews
    Dim N_IDs
    Dim strN_ID
    Dim CountN_ID
    Dim Count1N_ID
    Dim NewRndMax
    Dim NewRndNumber

    Set rsNews = Server.CreateObject("ADODB.Recordset")
    rsNews.ActiveConnection = MM_kdata_STRING
    rsNews.Source = "SELECT * FROM News"
    rsNews.CursorType = 1
    rsNews.CursorLocation = 2
    rsNews.LockType = 3
    rsNews.Open()

    rsNews.MoveLast
    CountN_ID = rsNews.RecordCount
    Count1N_ID = CountN_ID
    NewRndMax = CountN_ID

    If 2 < CountN_ID Then
    Count1N_ID = 2
    End If

    N_IDs = ","
    strN_ID = ","

    Do Until Count1N_ID = 0
    Randomize
    NewRndNumber = Int(Rnd * NewRndMax)

    If (InStr(1, strN_ID, "," & NewRndNumber & "," ) = 0) Then
    strN_ID = strN_ID & NewRndNumber & ","
    Count1N_ID = Count1N_ID - 1
    rsNews.MoveFirst
    rsNews.Move NewRndNumber
    N_IDs = N_IDs & rsNews("N_ID") & ","
    End If
    Loop
    rsNews.Close
    Set rsNews = Nothing
    '-----------------------------------------------------------
    Learning Guest

  16. #15

    Default Re: How to replace first and last "," in a string in asp vbscript?

    You can use the Replace function to replace something with a zero length
    string, or, in your case I'd use Left() and Right()

    That said, the code you have is pretty nasty. Are you just trying to find
    "x" random records from the database? If so, perhaps the following might
    work (not gaurenteed)

    <%
    strSQL = _
    "SELECT TOP 5 News_ID FROM News ORDER BY Rnd(-(1000*ID)*Now())"

    Set objRS = objConn.Execute strSQL
    If not objRS.EOF then
    arrResults = objRS.GetRows
    End If

    objRS.Close
    Set objRS = Nothing

    objConn.Close
    Set objConn = Nothing
    %>




    Cheers
    Ken

    "Learning" <question_asp@yahoo.com> wrote in message
    news:99a92f8.0307171043.baa312b@posting.google.com ...
    : I have downloaded the vb documents. Now I know that I can use Instrv()
    : and InstrvRev() to get the positions.
    :
    : MyString = ",1,2,3,4,5,6,7,8,"
    : MyString = Instr(MyString, ",") ' Position 1
    : MyString = InstrRev(MyString, ",") ' Position 17
    :
    : But after getting the positions how do I remove the "," ?
    : Do I need another function to remove them?
    : I am confused.
    : That would be great if you can help.
    :
    : Here is the code.
    : '----------------------------------------------------------
    : Dim rsNews
    : Dim N_IDs
    : Dim strN_ID
    : Dim CountN_ID
    : Dim Count1N_ID
    : Dim NewRndMax
    : Dim NewRndNumber
    :
    : Set rsNews = Server.CreateObject("ADODB.Recordset")
    : rsNews.ActiveConnection = MM_kdata_STRING
    : rsNews.Source = "SELECT * FROM News"
    : rsNews.CursorType = 1
    : rsNews.CursorLocation = 2
    : rsNews.LockType = 3
    : rsNews.Open()
    :
    : rsNews.MoveLast
    : CountN_ID = rsNews.RecordCount
    : Count1N_ID = CountN_ID
    : NewRndMax = CountN_ID
    :
    : If 2 < CountN_ID Then
    : Count1N_ID = 2
    : End If
    :
    : N_IDs = ","
    : strN_ID = ","
    :
    : Do Until Count1N_ID = 0
    : Randomize
    : NewRndNumber = Int(Rnd * NewRndMax)
    :
    : If (InStr(1, strN_ID, "," & NewRndNumber & "," ) = 0) Then
    : strN_ID = strN_ID & NewRndNumber & ","
    : Count1N_ID = Count1N_ID - 1
    : rsNews.MoveFirst
    : rsNews.Move NewRndNumber
    : N_IDs = N_IDs & rsNews("N_ID") & ","
    : End If
    : Loop
    : rsNews.Close
    : Set rsNews = Nothing
    : '-----------------------------------------------------------


    Ken Schaefer Guest

  17. #16

    Default Re: How to replace first and last "," in a string in asp vbscript?

    I tried:

    MyString = ",1,2,3,4,5,6,7,8,"
    MyString = mid(MyString,2,len(MyString)-2)

    and it works fine for me. :)
    Thanks to all of you who took some time to reply and I really
    appreciate your help. :)

    Bab
    Learning Guest

  18. #17

    Default Re: How to replace first and last "," in a string in asp vbscript?

    Function xTrim(lookWhere,trimWhat)

    Do While left(lookWhere,len(trimWhat))=trimWhat : lookWhere = right(lookWhere,len(lookWhere)-len(trimWhat)) : Loop
    Do While right(lookWhere,len(trimWhat))=trimWhat : lookWhere = left(lookWhere,len(lookWhere)-len(trimWhat)) : Loop

    xTrim = lookWhere
    end function

    Mystring = xTrim(Mystring,",")


    Quote Originally Posted by Learning View Post
    How can I replace first as well as last "," in a string?

    Mystring = ",3,2,5,6,1,65,22,"

    I would like
    CorrectedString = "3,2,5,6,1,65,22"

    I would like to replace commas on each sides with "".
    I am new to asp and would be grateful for your help.
    Thanks in advance.
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139