Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String value in C#

String value in C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pierre besquent
    wrote on last edited by
    #1

    Hi everyBody; I want to browse my string like this:

    // Extraire la suite de Zero au début du nombre.
    int k = 0;
    char car = ' ';
    string successZero = string.Empty;
    char[] arr;
    arr = chaine.ToCharArray(0, chaine.Length);
    if (arr [0] != '0')
    {
    while (k < chaine.Length)
    {
    car = arr [k];
    if (car == '0')
    {
    successZero += car;
    car = ' ';
    k ++;
    }
    else
    break;
    }
    }
    else
    successZero = "";
    return successZero;

    but if I enter "001" the arr [0]= 48 '0' and NOT '0' only I don't know it appears to me that 48 (I know it is the ASCII of 0) but I want to discutate sur arr[0] == '0' or not How to remidiate to that? ty

    L B 2 Replies Last reply
    0
    • P Pierre besquent

      Hi everyBody; I want to browse my string like this:

      // Extraire la suite de Zero au début du nombre.
      int k = 0;
      char car = ' ';
      string successZero = string.Empty;
      char[] arr;
      arr = chaine.ToCharArray(0, chaine.Length);
      if (arr [0] != '0')
      {
      while (k < chaine.Length)
      {
      car = arr [k];
      if (car == '0')
      {
      successZero += car;
      car = ' ';
      k ++;
      }
      else
      break;
      }
      }
      else
      successZero = "";
      return successZero;

      but if I enter "001" the arr [0]= 48 '0' and NOT '0' only I don't know it appears to me that 48 (I know it is the ASCII of 0) but I want to discutate sur arr[0] == '0' or not How to remidiate to that? ty

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I'm not sure what you are trying to do but your code does not seem to correspond to your description. You are looking for a zero in the arry (maybe), or trying to count them, but the line:

      if (arr [0] != '0')

      means that you never enter the search loop if the first character of your string is '0', as is the case you describe.

      The best things in life are not things.

      P 1 Reply Last reply
      0
      • L Lost User

        I'm not sure what you are trying to do but your code does not seem to correspond to your description. You are looking for a zero in the arry (maybe), or trying to count them, but the line:

        if (arr [0] != '0')

        means that you never enter the search loop if the first character of your string is '0', as is the case you describe.

        The best things in life are not things.

        P Offline
        P Offline
        Pierre besquent
        wrote on last edited by
        #3

        Hi, yes that what I search arr [0] appears to me always != '0' even I enter chaine = "001" it appears to me arr [0] = 48 '0' what can I do? ty

        R L 2 Replies Last reply
        0
        • P Pierre besquent

          Hi everyBody; I want to browse my string like this:

          // Extraire la suite de Zero au début du nombre.
          int k = 0;
          char car = ' ';
          string successZero = string.Empty;
          char[] arr;
          arr = chaine.ToCharArray(0, chaine.Length);
          if (arr [0] != '0')
          {
          while (k < chaine.Length)
          {
          car = arr [k];
          if (car == '0')
          {
          successZero += car;
          car = ' ';
          k ++;
          }
          else
          break;
          }
          }
          else
          successZero = "";
          return successZero;

          but if I enter "001" the arr [0]= 48 '0' and NOT '0' only I don't know it appears to me that 48 (I know it is the ASCII of 0) but I want to discutate sur arr[0] == '0' or not How to remidiate to that? ty

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          This is simply a viewing thing in your IDE. arr[0] will be '0' in this case. The problem is that if arr[0] is '0', the outer if fails, and if it is not '0', the if inside the while will fail on the first iteration and break out of the loop. You have a simple logic problem. I think the outer if is entirely unnecessary (even if it is corrected).

          P 1 Reply Last reply
          0
          • P Pierre besquent

            Hi, yes that what I search arr [0] appears to me always != '0' even I enter chaine = "001" it appears to me arr [0] = 48 '0' what can I do? ty

            R Offline
            R Offline
            Rhuros
            wrote on last edited by
            #5

            What are you expecting arr[0] to be? With you input of "001" you can expect your array of characters to be:# arr[0] = '0' arr[1] = '0' arr[2] = '1' So in this case your code is going to me the condition of (arr[k] != '0') on the 3rd iteration of your loop, not on the first. Is this what you’re looking for?

            1 Reply Last reply
            0
            • B BobJanova

              This is simply a viewing thing in your IDE. arr[0] will be '0' in this case. The problem is that if arr[0] is '0', the outer if fails, and if it is not '0', the if inside the while will fail on the first iteration and break out of the loop. You have a simple logic problem. I think the outer if is entirely unnecessary (even if it is corrected).

              P Offline
              P Offline
              Pierre besquent
              wrote on last edited by
              #6

              Hi, viewing thing in your IDE?what u mean. I debug and I found that arr[0] = 48 '0' even if chaine = "001". I know what i write so this it is my probleme: WHY it considerate arr [0] = 48 '0'?????? ty

              M B 2 Replies Last reply
              0
              • P Pierre besquent

                Hi, yes that what I search arr [0] appears to me always != '0' even I enter chaine = "001" it appears to me arr [0] = 48 '0' what can I do? ty

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Pierre besquent wrote:

                yes that what I search arr [0] appears to me always != '0' even I enter chaine = "001"

                No it doesn't; your coding logic is wrong as I stated in my previous answer. I have a feeling something is still getting lost in translation here, but I cannot figure it out.

                The best things in life are not things.

                1 Reply Last reply
                0
                • P Pierre besquent

                  Hi, viewing thing in your IDE?what u mean. I debug and I found that arr[0] = 48 '0' even if chaine = "001". I know what i write so this it is my probleme: WHY it considerate arr [0] = 48 '0'?????? ty

                  M Offline
                  M Offline
                  mabo42
                  wrote on last edited by
                  #8

                  Your debugger is showing to you that arr[0] contains char '0' which is the same as short 48.

                  short number = 48;
                  char letter = '0';
                  Console.WriteLine(number == letter); // --> prints True

                  Hope, this makes is clear.

                  P 1 Reply Last reply
                  0
                  • M mabo42

                    Your debugger is showing to you that arr[0] contains char '0' which is the same as short 48.

                    short number = 48;
                    char letter = '0';
                    Console.WriteLine(number == letter); // --> prints True

                    Hope, this makes is clear.

                    P Offline
                    P Offline
                    Pierre besquent
                    wrote on last edited by
                    #9

                    Hi, thanks for the clarification but still not clear HOW to test if arr [0] == '0' or not?? still my debugger returns to me arr [0] = 48 '0'. ty

                    L 1 Reply Last reply
                    0
                    • P Pierre besquent

                      Hi, viewing thing in your IDE?what u mean. I debug and I found that arr[0] = 48 '0' even if chaine = "001". I know what i write so this it is my probleme: WHY it considerate arr [0] = 48 '0'?????? ty

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #10

                      You are debugging in an IDE. It is showing you the integer value as well as the character, in case it is an unprintable character. arr[0] is still equal to '0' and the problem is that the two tests in your code contradict each other. (edit: oops, not a BBcode forum.)

                      1 Reply Last reply
                      0
                      • P Pierre besquent

                        Hi, thanks for the clarification but still not clear HOW to test if arr [0] == '0' or not?? still my debugger returns to me arr [0] = 48 '0'. ty

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Pierre besquent wrote:

                        HOW to test if arr [0] == '0' or not??

                        Well you could use a simple expression like:

                        if (arr[0] == '0')
                        // or
                        if (arr[0] == 0x30)
                        // or
                        if (arr[0] == 48)

                        You need to try and clarify your question as it is still not obvious to us what your problem is.

                        The best things in life are not things.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups