Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C#, I want to use a variable to sort characters using string interpolation

I am sorry for the answerer who will suffer from my poor English skills.

static void function(int numSpace)
{
    Console.WriteLine($"{Convert.ToString(num, 16), numSpaces}); 
    .....
    .....
} 

I want to use the value of the ‘numSpace’ variable as a value that specifies how many spaces to space when sorting strings. The problem is that only constants are possible. I want to set this as a variable in the Main function to set the number of spaces to be spaced through keyboard input.

How could this be possible?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To solve this problem, I tested by referring to several materials posted on ‘stack overflow’, but all failed.

I wrote this code to make this possible.

const int num = numSpace;

But I found out that this doesn’t work.

>Solution :

The string interpolation feature is limited to padding to a fixed amount with whitespace only, so you’ll have to pad yourself, remembering that a positive alignment right-justifies the string:

var padded = Convert.ToString(num, 16).PadLeft(numSpaces);
Console.WriteLine($"{padded}");  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading