String value in C#
-
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
-
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
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.
-
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.
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
-
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
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).
-
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
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? -
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).
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
-
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
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.
-
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
-
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 TrueHope, this makes is clear.
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
-
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
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.)
-
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
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.