Results 1 to 4 of 4

Thread: assign a string to an array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260

    assign a string to an array

    I am looking for a fast and effeciant way of assignning a string of characters to an array.

    I have tried this and it works ..
    Alpha = System.Text.RegularExpressions.Regex.Split( "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", "," )

    but there must be a better way. This seems very clunky.

    gollnick
    William E Gollnick

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I'm not sure what you mean by clunky but the same function is available in the String object (but its not shared so you'll need an instance of a string)
    VB Code:
    1. Dim alpha() As String
    2.         Dim list As String = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
    3.         alpha = list.Split(",")
    Or you can assign them to the array when you declare it
    VB Code:
    1. Dim alpha() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Hendersonville , NC
    Posts
    260
    Ok .. but lets say the character string I get in is not comma seperated .... Just a straight datastream like...

    "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    and I want this in an array where I can navigate to any single character in this string.... I cant to know that element 12 of this array is the letter "m"

    gollnick
    William E Gollnick

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If there is no seperator then things may get more complicated. Although if you are strictly talking about chars then its even simplier than an array. A string is really just an array of chars so if the datastream is made of chars then you can assign it to a string and retrieve info vis the indexOf or SubString methods.
    VB Code:
    1. Dim datastream As String = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    2.         'get char at location 12
    3.         MsgBox(datastream.Substring(12,1))
    4.         'what index is 'm' returns -1 if not found
    5.         MsgBox(datastream.IndexOf("m"))

Posting Permissions

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



Click Here to Expand Forum to Full Width