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]);
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:
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:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 5 years ago.
I want to separate a string and put it into an array.
Example:
id = 12,32,544,877,136,987
arraylist: [0]-->12
[1]-->32
[2]--544
[3]-->877
[4]-->136
[5]-->987
How to do that?
If your id var is a String, you can use the Split method:
id.Split(',')
Try:
string[] arraylist = id.Split(',');
Can do something like this in java :
ArrayList<String> idList = new ArrayList <String>();
String id = "12,32,544,877,136,987";
String idArr[] = id.split(",");
for(String idVal: idArr){
idList.add(idVal);
}
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();
This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 9 years ago.
I have a string that could look like this: smithj_Website1 or it could look like this rodgersk_Website5 etc, etc. I want to be able to store in a string what is after the "_". So IE (Website1, Website5,..)
Thanks
Should be a simple as using substring
string mystr = "test_Website1"
string theend = mystr.SubString(mystr.IndexOf("_") + 1)
// theend = "Website1"
mystr.IndexOf("_") will get the position of the _ and adding one to it will get the index of the first character after it. Then don't pass in a second parameter and it will automatically take the substring starting at the character after the _ and stopping and the end of the string.
int startingIndex = inputstring.IndexOf("_") + 1;
string webSite = inputstring.Substring(startingIndex);
or, in one line:
string webSite = inputstring.Substring(inputstring.IndexOf("_") + 1);