假设我有一个字符串:
string mystring = "34234234d124";
Run Code Online (Sandbox Code Playgroud)
我想得到这个字符串的最后四个字符"d124".我可以使用SubString,但它需要几行代码.
是否可以使用C#在一个表达式中获得此结果?
我正在编写一个简单的导入应用程序,需要读取CSV文件,在a中显示结果DataGrid并在另一个网格中显示CSV文件的损坏行.例如,显示另一个网格中短于5个值的行.我试着这样做:
StreamReader sr = new StreamReader(FilePath);
importingData = new Account();
string line;
string[] row = new string [5];
while ((line = sr.ReadLine()) != null)
{
row = line.Split(',');
importingData.Add(new Transaction
{
Date = DateTime.Parse(row[0]),
Reference = row[1],
Description = row[2],
Amount = decimal.Parse(row[3]),
Category = (Category)Enum.Parse(typeof(Category), row[4])
});
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,在阵列上操作非常困难.有没有更好的方法来分割价值观?
我有以下数据:
D:\toto\food\Cloture_49000ert1_10_01_2013.pdf
D:\toto\food\Cloture_856589_12_01_2013.pdf
D:\toto\food\Cloture_66rr5254_10_12_2012.pdf
Run Code Online (Sandbox Code Playgroud)
如何提取日期部分?例如:
D:\toto\food\Cloture_49000ert1_10_01_2013.pdf --> 10_01_2013
D:\toto\food\Cloture_856589_12_01_2013.pdf --> 12_01_2013
D:\toto\food\Cloture_66rr5254_10_12_2012.pdf --> 10_12_2012
Run Code Online (Sandbox Code Playgroud)
我的想法是使用LastIndexOf(".pdf") 然后倒数10个字符.
如何使用子串或其他方法解决这个问题?
在VB中有一个名为Right的函数,它返回一个字符串右侧包含指定数量字符的字符串.
在C#中是否有类似的功能可以做同样的事情?
谢谢.
使用C#在Visual Studio环境中添加"Microsoft Visual Basic"作为参考有什么好处?
如何从字符串中删除字母并只留下最后 3 个字符
输入:
foobar
Run Code Online (Sandbox Code Playgroud)
输出:
bar
Run Code Online (Sandbox Code Playgroud)
我试过:
string.Concat("foobar".Reverse().Skip(3).Reverse());
Run Code Online (Sandbox Code Playgroud)
但这删除了最后 3 行并没有保留它们