Answered Negative Indexes in C#

BlackBeard

Member
Joined
Oct 27, 2020
Messages
5
Programming Experience
Beginner
I'd like to perform a negative index in C#, I have tried a couple of times but seems it not working .
Can anyone help ?
 
Solution
So you already got a string from Console.ReadLine(). So why go through that crazy conversion from a string to an int, and then back to a string again?

Also, unless you are planning on changing the characters of a string, there is no need to make the string into an char array. This should cover what you need:
C#:
Console.WriteLine("Enter number = ");
string input = Console.ReadLine();

if (!int.TryParse(input, out int value))
    Console.Error.WriteLine($"{input} is not a number.");

// Look at each character of the input:
for(int i = 0; i < input.Length; i++)
{
    char ch = input[i];
    Console.WriteLine($"input[{i}] is '{ch}'");
}

I'm still not seeing why you would need negative indexes.

As a quick aside, if...
Welcome to the forums.

Please explain where you are attempting this, why you are attempting this, and how you are attempting this, and provide the code you are using and put that code in the provided code tags block : [CODE=csharp] Your code here [/CODE].
 
Thanks Sheepings ,

Okay I am new to C# and programming as a whole , I attempting to solve this question where I am demanded to access the last digit of a user input .
For example 56772 to and I need to get 2 from the numbers and store it in a variable.

I think I must share my code ;
Determining the last value of a user input:
int num ;
Console.Writeline("Enter number = ");
num = Convert.ToInt32(Console.ReadLine());

// My intention is to convert the int input first to string before and array
//Don't know if I am right.:)

num = num.ToString();
num = To.Array();


// I'm stuck
 
So you already got a string from Console.ReadLine(). So why go through that crazy conversion from a string to an int, and then back to a string again?

Also, unless you are planning on changing the characters of a string, there is no need to make the string into an char array. This should cover what you need:
C#:
Console.WriteLine("Enter number = ");
string input = Console.ReadLine();

if (!int.TryParse(input, out int value))
    Console.Error.WriteLine($"{input} is not a number.");

// Look at each character of the input:
for(int i = 0; i < input.Length; i++)
{
    char ch = input[i];
    Console.WriteLine($"input[{i}] is '{ch}'");
}

I'm still not seeing why you would need negative indexes.

As a quick aside, if you know that the input is an integer, you could just divide by 10 and get the remainder using the modulo operator to get the last digit.
 
Solution
So you already got a string from Console.ReadLine(). So why go through that crazy conversion from a string to an int, and then back to a string again?

Also, unless you are planning on changing the characters of a string, there is no need to make the string into an char array. This should cover what you need:
C#:
Console.WriteLine("Enter number = ");
string input = Console.ReadLine();

if (!int.TryParse(input, out int value))
    Console.Error.WriteLine($"{input} is not a number.");

// Look at each character of the input:
for(int i = 0; i < input.Length; i++)
{
    char ch = input[i];
    Console.WriteLine($"input[{i}] is '{ch}'");
}

I'm still not seeing why you would need negative indexes.

As a quick aside, if you know that the input is an integer, you could just divide by 10 and get the remainder using the modulo operator to get the last digit.
Thank you so much Skydiver, I am new to to programming. Kindly understand my ignorance.

Once again thank you, there is a lot I have to learn.
 
And yeah, if you are using C# 8, you could also get the Python-esque negative indices to get the last character using the ^value syntax. Ex.
C#:
string input = "12345";
char last = input[^1];
Console.WriteLine(last);    // prints out 5
 
And yeah, if you are using C# 8, you could also get the Python-esque negative indices to get the last character using the ^value syntax. Ex.
C#:
string input = "12345";
char last = input[^1];
Console.WriteLine(last);    // prints out 5
Haha..Very simple . Thanks
 
Be warned that it's only available on newer version of the language (C# 8 or higher). If you are using the .NET Framework 4.8 or lower, or .NET Core 2.1 or lower, or .NET Standard 2.0 or lower, you'll be using C# 7.x. No handy indexing using ^value readily available there.
 
there is no need to make the string into an char array.
An an aside, and in our OP's defence, I must admit that I have often done this myself under extreme tiredness. You of all people know how hard I work, and the hours I work for what I'm building. :) My last shift was three days straight including my own personal/client projects. It's a very simple mistake to make when under a heavy workload or great pressure. My point is little mistakes happen to the best of us! :cool:
 
Back
Top Bottom