string[0] get first character from string [duplicate] - c#

This question already has answers here:
What does C# do when typecasting a letter to an int?
(5 answers)
Closed 6 years ago.
I have a problem which I do not understand. Here is my code:
String input = "3 days ago"
String firstCharacter = input[0].ToString(); //Returns 3
int firstCharacter = (int)input[0]; //Returns 51
Why does it return 51?
PS: My code comes from this thread: C#: how to get first char of a string?
More information:
In case that input = "5 days ago", then int firstCharacter is 53.

Casting a char to int in this way will give you its ASCII value which for 3 is equal to 51. You can find a full list here:
http://www.ascii-code.com/
You want to do something like this instead:
Char.GetNumericValue(input[0]);

You can also use substring to extract it as a string instead of a char and avoid having to cast it:
string input = "3 days ago";
string sFirstCharacter = input.Substring(0, 1);
int nFirstCharacter = int.Parse(input.Substring(0, 1));

Related

Format string with variable in HEX format [duplicate]

This question already has answers here:
Format int to hex string
(2 answers)
Closed 1 year ago.
I know I can include variables into string:
int a =1
String s = $"a={a}";
This will show line:
a=1
But how to show answer in HEX format as below?
a=0x01
int a = 1;
string s = $"a=0x{a:x2}";

c# - How can i format a string in c# [duplicate]

This question already has answers here:
Add zero-padding to a string
(6 answers)
Closed 4 years ago.
I have a very simple Question to ask.
I have a string like:
string str="89";
I want to format my string as follow :
str="000089";
How can i achieve this?
Assuming the 89 is actually coming from another variable, then simply:
int i = 89;
var str = i.ToString("000000");
Here the 0 in the ToString() is a "zero placeholder" as a custom format specifier; see https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
If you have a string (not int) as the initial value and thus you want to pad it up to length 6, try PadLeft:
string str = "89";
str = str.PadLeft(6, '0');
If you want the input to be a string you'll have to parse it before you output it
int.Parse(str).ToString("000000")

C# Increment Alphabet character [duplicate]

This question already has answers here:
How to find out next character alphabetically?
(8 answers)
Closed 5 years ago.
I'm trying to increment the first character of a string to the next letter in the Alphabet.
I have found this:
string str_A1 = "A1";
string str_B;
str_B= str[0]++;
Here str_B should be "B" but I get an error saying:
Property or indexer 'string.this[int]' cannot be assigned to -- it is read only
You cannot modify the value of the first character of str_A1, which is what the ++ operator is doing. Do this instead:
str_B = ((char)(str[0] + 1)).ToString();
You can do it something like:
string str_A1 = "A1";
string str_B = (char)(str_A1[0] + 1) + str_A1.Remove(0, 1);

Hex long number to string that looks the same [duplicate]

This question already has answers here:
Convert a number into the hex value in .NET
(2 answers)
Closed 6 years ago.
I've got a simple question i have got a long value presented in this way
long value = 0x001f0347
Now is there's a way to convert it to string that looks the same:
string value = "0x001f0347";
I have tried some converters but no luck.
Try formatting ("x8" format string - 8 hexadecimal digits):
long value = 0x001f0347;
string result = "0x" + value.ToString("x8");
If you prefer Convert then convert using toBase == 16 and pad left up to 8 symbols
string result = "0x" + Convert.ToString(value, 16).PadLeft(8, '0');

How to get 4 digits from the middle a string in C# [duplicate]

This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 8 years ago.
I have a string variable like test10015, i want to get just the 4 digits 1001,
what is the best way to do it?
i"m working in asp.net c#
With Linq:
var expected = str.Skip(4).Take(4);
Without Linq:
var expected = str.Substring(4,4);
Select the first four digits in your string:
string str = "test10015";
string strNum = new string(str.Where(c => char.IsDigit(c)).Take(4).ToArray());
You can use String.Substring Method (Int32, Int32). You can subtract 5 from from the length to start from your required index. Make sure the format of string remains the same.
string res = str.Substring(str.Length-5, 4);
string input = "test10015";
string result = input.Substring(4, 4);

Categories