This question already has answers here:
Pad left with zeroes
(4 answers)
Closed 2 years ago.
I try to fill a string with 0 at left.
My variable :
string value = "0.5";
So my output is 0.5 and I want 00.5. This value can also be an integer like 450, needed as 0450.
I tried string.Format("{0:0000}", value); but it doesn't work.
I also tried string value = (double.Parse(value)).ToString("0000"); but it doesn't work.
You should use PadLeft method for that
var value = "0.5";
var result = value.PadLeft(4, '0'); //gives 00.5
Related
This question already has answers here:
.NET String.Format() to add commas in thousands place for a number
(23 answers)
Closed 2 years ago.
In my program, I'm trying to add a comma for a number that's length is more than 3. For example if number is 1000, it should output 1,000.
However I can't find out how to add on remaining numbers after I put comma into first one. The code below is what I've got:
// if the answer is more than 999
string answerThousand = Convert.ToString(lbl_NumResult.Text);
if (answerThousand.Length > 3)
{
lbl_NumResult.Text = answerThousand[1] + "," + answerThousand[ /* What must be here to add remaining numbers? */];
}
You could just pass a formatter to the ToString method:
decimal inputValue = 0;
if (decimal.TryParse(lbl_NumResult.Text, out inputValue))
{
string answerThousand = inputValue.ToString("N", CultureInfo.InvariantCulture);
}
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#NFormatString
This question already has answers here:
Convert int to string?
(12 answers)
Closed 4 years ago.
I am currently splitting a string from a textbox that the user will fill with three numbers. Those numbers i want to be saved as seperate integers. Any help as to how to do this? Thanks for any help!
string[] count = txtPoemInput.Text.Split('/'); //Splitting values for keyword numbers
int Poem, Line, Word;
count[0] = Poem.ToString; // Example
count[1] = Line; // Example
count[2] = Word;
Here is what you need to do. Use Convert.ToInt32
Poem = Convert.ToInt32(count[0]);
Line = Convert.ToInt32(count[1]);
Word = Convert.ToInt32(count[2]);
This question already has answers here:
Is there an easy way to return a string repeated X number of times?
(21 answers)
How to repeat a set of characters
(3 answers)
Closed 4 years ago.
I want to insert a number of * before a string based on an items depth and I'm wondering if there is a way to return a string repeated Y times. Example:
string indent = "***";
Console.WriteLine(indent.Redraw(0)); //would print nothing.
Console.WriteLine(indent.Redraw(1)); //would print "***".
Console.WriteLine(indent.Redraw(2)); //would print "******".
Console.WriteLine(indent.Redraw(3)); //would print "*********".
You can use the String constructor:
string result = new String('*', 9); // 9 *
If you really want to repeat a string n-times:
string indent = "***";
string result = String.Concat(Enumerable.Repeat(indent, 3)); // 9 *
Yes you are looking for PadLeft() Or, in your case even PadRight() will do the trick :
string indent = "".PadLeft(20, '*'); //repeat * 20 times
This question already has answers here:
How can I format a number into a string with leading zeros?
(11 answers)
C# convert int to string with padding zeros?
(15 answers)
Closed 4 years ago.
I know I can format string with left padding with spaces with line:
int i=5;
serial = String.Format("{0,20}", i);
But how to left pad wit 0 in order to get string below?
00000000000000000005
You can use the built-in function PadLeft:
int i = 5;
var result = i.ToString().PadLeft(8, '0');
Note: this doesn't work for negative numbers (you'd get 00000000-5).
For that you can use the built-in formatters:
int i = 5;
var result = string.Format("{0:D8}", i);
This question already has answers here:
Get Second to last character position from string
(10 answers)
Closed 5 years ago.
* I cannot delete this duplicate question because someone has answered it *
I have file names formatted CustomerInfoDaily.12042014.080043 and CustomerInfoDaily.A.12042014.080043 I'm trying to get the base name (CustomerInfoDaily) and the base suffix (.12042014.080043) using substrings. There is no limit to the number of periods however the suffix is always .\d{8}.\d{8}
string fn = "CustomerInfoDaily.A.12042014.080043";
string baseFileName = fn.Substring(0, fn.LastIndexOf(".",fn.Length-1,fn.Length));
string baseSuffix = fn.Substring(fn.LastIndexOf(".", 0, 2));
The problem is that you can say you want the first or last dot but there is no saying that you want the second to the last instance of the dot.
Any help or advice would be greatly appreciated.
Consider using string.Split:
string fn = "CustomerInfoDaily.A.12042014.080043";
var split = fn.Split('.');
var last = split.LastOrDefault();
var secondLast = split.Skip(split.Length - 2).FirstOrDefault();