I need to know in which position is the decimal point in my String, for example:
1,500(string string) in this case the "." would be at position [1].
I'm trying this but it doesn't work:
public int funcion(String number)
{
int tam = number.Length;
int pos = 0;
for (int i = 0; i < tam; i++)
{
if (number[i].Equals("."))
{
return pos;
}
pos++;
}
return pos = -1;
}
You are comparing against a string using
""
. You have to use ' since what you want is a char.
Although the result is correct, you can also use the method
IndexOf
:That method returns the index of a character in a string or -1 if it doesn't exist.
Remember that it will throw an exception if the string is empty or null. I would leave it like this:
You must change "." by '.' since what is expected is a char, remember that a character of a string is going to be a char, so the same would happen with any other. Being that way: