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")
Related
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}";
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance
value = value.Replace( ", ", string.Empty);
strings in .net are immutable.
Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);
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');
This question already has answers here:
Get Substring - everything before certain char
(9 answers)
Closed 8 years ago.
Lets say I have a string:
string a = "abc&dcg / foo / oiu";
now i would like the output to be
"abc&dcg"
i have tried:
string output= a.Substring(a.IndexOf('/'));
but it returns the last part not the first part
I have tried trim() as well, but doesn't provide me with the results.
Try this:
string result = a.Split('/')[0].Trim();
The split operation will give you the 3 substrings separated by '/' and you can choose whichever ones you want by specifying the index.
Try this one
string a = "abc&dcg / foo / oiu";
string output = a.Substring(0, a.IndexOf("/"));
Console.WriteLine(output);
It will show
abc&dcg
Try
string output;
if (a.IndexOf('/')>=0) { output = a.Split('/')[0].Trim() };
This wil prevents error case a doesn't contains any /
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);