String error

Discussion in 'C#' started by Carlos, Mar 2, 2009.

  1. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    Hello everyone!

    Please! check this code.

    string word="It is a string";
    while (word != '\0')
    {
    Console.Write(word+"\n");
    i++;
    }

    It give an unexpected error, after printing all the letters.
    Can anyone tell me how to remove this error?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    In C# string is not character array which you can and should loop through
     
  3. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    So, how can i break this string into characters without error.
    string word;
    Console.Write(word+"\n");

    Please! Write some code for it.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Do you want to break it up into characters or print the entire string?
     
  6. Carlos

    Carlos New Member

    Joined:
    Dec 5, 2008
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    none
    I want to break the string into characters, by using while or For loop, and the loop should stop as the string ends.
    My Code is breaking the string but it give error in the last.

    I can also use the condition word.length() but i want to do it as it is done in C++.
    while (word != '\0')
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Try the Substring function o the [] operator but do not loop it like in C.

    Loop it using the length variable
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice