1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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,287
    Likes Received:
    364
    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,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    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,287
    Likes Received:
    364
    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