whats the best way to do this?

Discussion in 'ASP .Net' started by Steve, Jul 12, 2007.

  1. Steve

    Steve Guest

    I have to create 2 strings and then parse one string out to save the data
    into the database.

    My first string looks like this:

    John|Smith|,Greg|Henry|,Kelly|Smith|,

    I then need to parse out that string to seperate them by the commas so I see
    this
    John|Smith|
    Greg|Henry|
    Kelly|Smith|


    I then need to parse out that string so I get
    John
    Greg
    Kelly

    Smith
    Henry
    Smith

    so I can save each value to the database, what is the best way to do this?
    The stored procedure I'm calling is already parsing strings by the comma, so
    would it be easier to just parse out the large string in code and pass the
    smaller string with the pipe (|) and make a change to the proc to parse the
    values by the (|) or should I do all of this in my C# code? If the C# way,
    how would that be done? I'm able to get this string format
    (John|Smith|) but when I try to parse it out, I' get errors
    (Index was outside the bounds of the array)
     
    Steve, Jul 12, 2007
    #1
    1. Advertisements

  2. Steve

    Alex Meleta Guest

    Alex Meleta, Jul 12, 2007
    #2
    1. Advertisements

  3. Steve

    Steve Guest

    I am currently doing that but when I split the first string by the comma(,)
    its fine, its the second string John|Smith| thats giving me
    a fit.

    when i do this:

    String[] names= ViewState["custnames"].ToString().Split('|');
    foreach (String n in names)
    {
    String[] u = n.ToString().Split('|');
    Response.Write(u[0]);
    Response.Write(u[1]);
    }

    I'm getting this error:
    Index was outside the bounds of the array, but if I do this
    String[] names= ViewState["custnames"].ToString().Split('|');
    foreach (String n in names)
    {
    String[] u = n.ToString().Split('|');
    Response.Write(u[0]);
    }

    I can see the first name in the string with no problem. So am I missing
    something or doing something wrong in the sytnax?
     
    Steve, Jul 12, 2007
    #3
  4. strRaw =
    "John|Smith|,Greg|Henry|,Kelly|Smith|,";

    foreach (string strRecord in strRaw.Split(','))
    {
    string[] astrElements = strRecord.Split('|');

    strFirstName = astrElements[0];
    strLastName = astrElements[1];
    strEmailAddress = astrElements[2];
    }
     
    Mark Rae [MVP], Jul 12, 2007
    #4
  5. Steve

    Steve Guest

    Mark,
    I've tried that, I've tried everything I can think of and it only shows
    [0] and anything beyond that give me the out of bounds error message.


     
    Steve, Jul 12, 2007
    #5
  6. Not sure what's happening, then - I've just tried it, and it works fine for
    me.

    Can you post your whole code...
     
    Mark Rae [MVP], Jul 12, 2007
    #6
  7. Steve

    Steve Guest

    sure:

    code:
    main string:
    John|Smith|,Greg|Henry|,Kelly|Smith|,

    String[] users= ViewState["Stuff"].ToString().Split(',');

    which returns this:
    John|Smith|,

    then I loop through that string;
    foreach (string s in users)
    {
    string[] s= strRecord.Split('|');

    strFirstName = s[0];
    strLastName = s[1];
    strEmail = s[2];
    }
    Response.Write(strFirstName + " " + strLastName + " " +
    strEmail);

    and I get this error:
    Index was outside the bounds of the array.
    strLastName = astrElements[1];

     
    Steve, Jul 12, 2007
    #7
  8. Steve

    Alex Meleta Guest

    Hi Steve,

    Probably the exception occurs in that piece of code:
    S> Response.Write(u[1]);

    You should check the boundaries.
    Anyway, you can try to analyse that input string is correct, for doing this
    change your code to something like code below:

    foreach (String n in names)
    {
    foreach(String justForTest in n.ToString().Split('|'))
    { Response.WriteLine(justForTest); }
    Response.WriteLine("-");
    }

    Regards, Alex Meleta
    [TechBlog] http://devkids.blogspot.com



    S> I am currently doing that but when I split the first string by the
    S> comma(,) its fine, its the second string John|Smith|
    S> thats giving me a fit.
    S>
    S> when i do this:
    S>
    S> String[] names= ViewState["custnames"].ToString().Split('|');
    S> foreach (String n in names)
    S> {
    S> String[] u = n.ToString().Split('|');
    S> Response.Write(u[0]);
    S> Response.Write(u[1]);
    S> }
    S> I'm getting this error:
    S> Index was outside the bounds of the array, but if I do this
    S> String[] names= ViewState["custnames"].ToString().Split('|');
    S> foreach (String n in names)
    S> {
    S> String[] u = n.ToString().Split('|');
    S> Response.Write(u[0]);
    S> }
    S> I can see the first name in the string with no problem. So am I
    S> missing something or doing something wrong in the sytnax?
    S>
    S> S>
     
    Alex Meleta, Jul 12, 2007
    #8
  9. Steve

    Steve Guest

    I've done that and I can see the string that is being passed

    John|Smith|,Greg|Henry|,Kelly|Smith|,

    I can see that just fine, hell i can even parse it out by the commas, its
    splitting this string:
    John|Smith|

    thats joking on me

     
    Steve, Jul 12, 2007
    #9
  10. <snip>

    Hmm - can't see anything obviously wrong with that...

    I'm clutching at straws now, but could you tell me what s.Length returns?
    I'm wondering if there's some sort of CultureInfo conflict here which has
    caused the Split method to fail to interpret the pipe symbol correctly... If
    s.Length returns 1 instead of 3, I think that might be the reason...
     
    Mark Rae [MVP], Jul 12, 2007
    #10
  11. Steve

    Steve Guest

    I'm only using the one string right now just to parse it out. I'm using this
    string only right now.
    John|Smith|,"


    this returns me 80
    foreach (string strRecord in userDetails)
    {
    string[] astrElements = strRecord.Split('|');
    Response.Write(astrElements[0].Length);
    }

    this returns me 520
    foreach (string strRecord in userDetails)
    {
    string[] astrElements = strRecord.Split('|');
    Response.Write(strRecord.Length);
    }

    this returns me 2
    String[] userDetails = "John|Smith|,"
    Response.Write(userDetails.Length);
     
    Steve, Jul 12, 2007
    #11
  12. Steve

    Alex Meleta Guest

    Hi Steve,

    S> John|Smith|,Greg|Henry|,Kelly|Smith|,
    Yes, indeed. Because you have the comma at the end of the string and it is
    an empty string. You even can split it by '|' but it will be still empty.
    So when you try to access it by indexer an exception will be thrown.

    So, you should do these:
    1. Set up a boundary checks to be sure that length of the array is enough
    for access by indexer
    2. Bring you string to appropriate format (use Trim methods for cutting or
    such) or check that string can be splitted or was splitted correctly.

    Hopefully it helps

    Regards, Alex Meleta
    [TechBlog] http://devkids.blogspot.com




    S>
     
    Alex Meleta, Jul 12, 2007
    #12
  13. Hi, Steve.

    For that string, this works for me:

    <%@ Page Language="C#" %>
    <script runat="server">
    void Page_Load(Object obj, EventArgs e)
    {
    string strRaw = "John|Smith|";
    string[] astrElements = strRaw.Split('|');

    string strFirstName = astrElements[0];
    string strLastName = astrElements[1];
    string strEmail = astrElements[2];

    lblFirstName.Text = strFirstName;
    lblLastName.Text = strLastName;
    lblEmail.Text = strEmail;

    }
    </script>
    <html>
    <body>
    <asp:Label ID="lblFirstName" Runat="server" Text=""></asp:Label><br />
    <asp:Label ID="lblLastName" Runat="server" Text=""></asp:Label><br />
    <asp:Label ID="lblEmail" Runat="server" Text=""></asp:Label><br />
    </body>
    </html>
    ------------




     
    Juan T. Llibre, Jul 12, 2007
    #13
  14. Steve

    Steve Guest

    I do that in this

    I'm doing that in this;

    string temp =
    "John|Smith|,Greg|Henry|,Kelly|Smith|"
    ;
    string strFirstName = string.Empty;
    string strLastName = string.Empty;
    string strEmail = string.Empty;

    string[] userDetails = temp.Split(',');

    foreach (string strRecord in userDetails)
    {
    string[] astrElements = strRecord.Split('|');

    strFirstName = astrElements[0];
    strLastName = astrElements[1];
    strEmail = astrElements[2];
    }
    Response.Write(strFirstName + " " + strLastName + " " +
    strEmail);

    but i'm only getting the last person in the string. Also if I use trim(),
    I'm getting errors when i compile
     
    Steve, Jul 12, 2007
    #14
  15. Steve

    Alex Meleta Guest

    Hi Steve,

    John|Smith|,Greg|Henry|,Kelly|Smith|
    *,*

    See ending comma. It gives you 4 string and the 4th is empty. You can prevent
    it by checking (as I mentioned before) but if it's an incorrect format it
    may be better to thrown your own exception to certainly define them.


    Regards, Alex Meleta
    [TechBlog] http://devkids.blogspot.com
     
    Alex Meleta, Jul 12, 2007
    #15
  16. That simply isn't possible! There aren't even 80 letters in that string,
    never mind 80 elements!

    Are you following the method I showed you?

    Specifically, you need first to split the string by the comma to give an
    array of strings for each record.

    Then you need to split each record by the pipe symbol to give an array of
    individual elements.

    I don't think you're doing that...
     
    Mark Rae [MVP], Jul 12, 2007
    #16
  17. Yes, that's very true.

    What I normally do is trim the string by the split character first
     
    Mark Rae [MVP], Jul 12, 2007
    #17
  18. Hmm - is it possible that each record is overwriting the previous one so
    that only the last record remains? I don't think Response.Write would do
    that, but we're starting to run out of options here!
    You will do, because C# is case-sensitive. Try this instead:
    string[] userDetails = temp.Trim(',').Split(',');
     
    Mark Rae [MVP], Jul 12, 2007
    #18
  19. re:
    !> but i'm only getting the last person in the string. Also if I use trim(),

    To get around that, after :

    string[] astrElements = strRecord.Split('|');

    ....iterate through the array :

    Response.Write(astrElements[0].ToString() + "<br />" );
    Response.Write(astrElements[1].ToString() + "<br />" );
    Response.Write(astrElements[2].ToString() + "<br />" );

    You can also resplit astrElements and parse the individual records.






     
    Juan T. Llibre, Jul 12, 2007
    #19
  20. Here's the complete parser example :

    <%@ Page Language="C#" %>
    <script runat="server">
    void Page_Load(Object obj, EventArgs e)
    {
    string temp = "John|Smith|,Greg|Henry|,Kelly|Smith|";
    string strFirstName = string.Empty;
    string strLastName = string.Empty;
    string strEmail = string.Empty;

    string[] userDetails = temp.Split(',');

    foreach (string strRecord in userDetails)
    {
    string[] astrElements = strRecord.Split('|');

    Response.Write(astrElements[0].ToString() + "<br />" );
    Response.Write (astrElements[1].ToString() + "<br />" );
    Response.Write (astrElements[2].ToString() + "<br />" );
    }
    }
    </script>
    <html>
    <body>
    </body>
    </html>

    ----------------

    You can also resplit astrElements on the pipe for the individual FirstName, LastName, Email items.





     
    Juan T. Llibre, Jul 12, 2007
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.