1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
private
void
button1_Click(
object
sender, EventArgs e)
{
int
n = 3;
//处理第几行
string
f =
@"D:\test.txt"
;
StreamReader sr =
new
StreamReader(f);
string
[] all = sr.ReadToEnd().Split(
new
char
[] {
'\r'
,
'\n'
}, StringSplitOptions.RemoveEmptyEntries);
string
line3 = all[n - 1];
string
[] ss = line3.Split(
new
char
[] {
' '
}, StringSplitOptions.RemoveEmptyEntries);
int
n0 = Convert.ToInt32(ss[1]) + 1;
//加1
sr.Close();
StreamWriter sw =
new
StreamWriter(f,
false
);
string
wl =
""
;
for
(
int
i = 0; i < all.Length; i++)
{
if
(i == n - 1)
{
wl = ss[0] +
" "
+ n0.ToString();
}
else
wl = all[i];
sw.WriteLine(wl);
}
sw.Flush();
sw.Close();
MessageBox.Show(
"ok"
);
}
例如:txt文件中有这样5行数据 学号 次数 001 0 002 0 003 0 004 0 005 0 用c#怎么实现让003这行的对应的次数加一? [ComVisibleAttribute(false)] public string[] Split(string[] separator,StringSplitOptions options) 参数 separator 类型:System.String[] 分隔此字符串中的子字符串的字符串数组、不包含分隔符的空数组或 null。 options 类型:System.StringSplitOptions 要省略返回的数组中的空数组元素,则为 RemoveEmptyEntries;要包含返回的数组中的空数组元素,则为 None。 返回值 类型:System.String[] 一个数组,其元素包含此字符串中的子字符串,这些子字符串由 separator 中的一个或多个字符串分隔。 有关更多信息,请参见“备注”一节。
|
确定文件中含有多少行的快捷方式
最新推荐文章于 2023-06-10 17:23:26 发布