Passing string to int to substring
I currently get the users number like this:
if (checkBox1.Che开发者_如何转开发cked)
{
rchars = numericUpDown1.Value.ToString();
}
else
{
rchars = "3";
}
string rchars; is a global variable.
So, I'm trying to remove the rchars from file names. For example the first three characters from the file name:
int num = Int32.Parse(rchars);
foreach (FileInfo name in fpaths.GetFiles("*.mp3")
{
string snub = name.Name.Substring(num);
MessageBox.Show(snub);
System.IO.File.Move(blah + name.Name, newblah + snub);
}
My question is how can I get "num" to work in a substring? Since I can't get it to be a value. Since I want to pass it from the numericUpDown. Add make "num" a value so I can remove the value from the file names.
Thanks.
Value property of NumericUpDown is Decimal - that is perhaps why you are having issues. In your if block, I would consider casting the Value Property of NumericUpDown object into an integer in the true part and use integer value 3 in its else part. There after, I would avoid parsing again and give it to Substring as is.
You say you want to remove all occurrences of rchars
from name
, so why are you using Substring
? If you want to remove the string rchars
from name
then just keep it as a string and use String.Replace
:
string snub = name.Name.Replace( rchars, String.Empty );
Also, the Value
property of a NumericUpDown
is a decimal
, not an int
.
are you looking to replace the value "3" within the name of a file with something else/remove it? Like this:
original file:
string fiename = "myfile3.mp3";
remove/replace selected number (in this case 3):
string num = "3";
filename.replace(num, "");
should end up with a filename "myfile.mp3"
精彩评论