SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple array question

    Hi,

    I have a string like this:

    string myString = Part1#Part2#Part3;

    I want to separate the string into an array by splitting via the '#' character.

    When I try the following code, it works:

    Code:
    		string myString = Part1#Part2#Part3;
    
    		string[] myArray= myString.Split(new Char[] { '#' });
    		
    		foreach (string myWord in myArray)
    		{
    			testbox1.Text += myWord + "<br />";
    		}
    But I don't want to just loop through all array items, I want to return a value for just the second item in the array. e.g:

    Code:
    string myString = Part1#Part2#Part3;
    
    		string[] myArray= myString.Split(new Char[] { '#' });
    
    testbox1.Text = "The second part of the array is: " + myArray[1] + "<br />";
    This just errors though.

    How do I refer to an item in an array specifically, without looping through everything?

    Thanks all!

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)
    1stly you can also just go like this:

    string[] myArray = myString.Split('#');

    myArray[1].ToString() should work just fine. As that is pretty much the standard

  3. #3
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Nightstalker,

    Still getting the same error though:

    System.IndexOutOfRangeException: Index was outside the bounds of the array.

    On the line where I use [1].

    I've tried writing the string out and it's definitely showing: Part1#Part2 but when I split this and try to return the second item in the array, it errors, saying it's outside the bounds of the array.

    Can you see what I'm doing wrong?

    Thanks

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. This will wake up the trolls, but as far as I'm concerned I would always place an if statment to check that array index 1 existed before I accessed it, and consider relying on Error Handling to catch this as pretty poor. My only exception would be if the variable myString was a const or damn close to that (If It comes from the user or a database or a config file, I would always check its bounds).

    2. Saddly, point 1, doesn't explain your error though. I would have thought 30 seconds with a debugger on the offending line, would clear up your problem.

    3. Which brings me on to point 3, programming without the debugger is like driving without your eyes open, yes of cource it can be done, but why the hell would you be doing it. If for example this is only happens on your production server, then re-install your dot net stuff on that computer - Or wipe the server and start again.


    I got a lot of ifs on the 3 steps above, and in no way do I think they are relevent to you, but instead they maybe relevent to you. Take the advice of the bits you like, if any, ignore the rest.

  5. #5
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My problem is that I'm very new to .NET and don't understand many of the concepts or even much of the syntax just yet, hence me asking for help for a seemingly trivial problem.

    The values come from a database, so I'm happy that I don't have to check that it exists. I just can't get this simple thing to work, and wondered if anyone with experience could point out where I am going wrong.

    I don't know how to use the debugger. In fact I know nothing about it at all, but I can try to find out about it. If it's really like driving with your eyes closed then it would be stupid to not use it (although I've built a whole dynamic site and CMS so far, and this has been my only major problem).

    Thanks for your pointers, I guess I'll have to read up about the dubugger.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh well, then even I can help with this one.

    1. Move cursor to code that you wish to debug.

    2. Press F9

    3. Press F5

    4. Use your lovely website

    5. When dot NET executes your line of code, the debugger will appear

    6. Enjoy.

  7. #7
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)
    Debugging is always a good idea, you will see exactly what is happening. Could be a problem with your char encoding and the string might display as a # in the browser, but when you view the source its encoded into something like Part1&#37;23Part2 and therefor your code doesnt find the hash. check what part 1 is in the array and ull probably find the full string there.

    PS. %23 is not the encoding for #, i just used it as and eg. Cnt remember now what the hex value is for #

  8. #8
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all. Everything looks fine, but it seems that because this array is nested inside another, it won't work. Am I right in saying that it's not possible to nest arrays in c#/.net?

  9. #9
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)
    im not sure what you mean by nest arrays. but you can have object arrays

    object[] myArr = new[10];

    then i guess you could save a string arrary into the object array, but im not sure as I have never had a need for something like that b4.

    I actually never use arrays anymore unless i have to. I love my generic lists... lol

  10. #10
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorted, not sure what was going wrong, but it was probably a typo or using a string instead of an array or something. Thanks for your help all.

  11. #11
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    England
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your using nested in the old-fashion programmers meaning, as having two arrays with the same name in scope, then yes you can do that, though you will get problems that you just experienced.

    If as NightStalker-DNS, believes, you mean multi-dimensional array, which I do know some languages like javascript use the word nested to mean multi-dimensions. Then yes c# handles multi-dimensional arrays just fine.

    If you come from the world of javascript, for example, then yes javascript uses some words that are incorrect in the context of c based languages including c#. 'Real' programmers don't consider javascript and the manuals associated to be using these terms correctly.


Bookmarks

Posting Permissions

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