我有一個包含 smth 的檔案,如下所示:
Elsa 12 15
Charlie 3 60
Mattew 4 0
我需要找到以名稱開頭的字串,然后用更改的值替換所有行。搜索前我不知道值。
例如,我知道,我需要將 Mattew 2 添加到他的第一個值,將 4 添加到第二個,然后檔案將是這樣的:
Elsa 12 15
Charlie 3 60
Mattew 6 4
謝謝。
uj5u.com熱心網友回復:
您可以使用此代碼將檔案讀入 Line 類 (name,firtvalue,secondValue) List ,使用它,然后將其再次寫入檔案
List Lines = new List();
contentFile.Split("\n").ToList().ForEach(line =>
{
string[] Values = line.Split(" ").ToArray();
Lines.Add(new Line { Name = Values[0], FirstValue = int.Parse(Values[1]), SecondValue = int.Parse(Values[2]) });
});
uj5u.com熱心網友回復:
您可以使用for回圈迭代檔案的每一行并使用string.StartsWith()方法找到所需的行。然后,如果需要的話線路被發現-只是提供重寫nameToFind和新的整數值newValue1和newValue2,他們相結合string.Join()和" "空格分隔。
例如,如果您想在 line 處為兩個數字設定新值。
static void EditValues(string nameToFind, int newValue1, int newValue2)
{
string file = @"S:\File.txt";
if (File.Exists(file))
{
string[] lines = File.ReadAllLines(file);
for (int i = 0; i < lines.Length; i )
if (lines[i].StartsWith(nameToFind))
{
// Just rewrite line with same name but new values
lines[i] = string.Join(" ", nameToFind, newValue1, newValue2);
// Optionally, if there is no more values to edit - you can break loop immediately
break;
}
// Save (write) lines array back to file.
File.WriteAllLines(file, lines);
}
}
簡化版的創始索引與Array.FindIndex. 請注意,如果檔案中的“Mattew”很少 - 在這種情況下只會編輯第一個。
static void EditValues(string nameToFind, int newValue1, int newValue2)
{
string[] lines = File.ReadAllLines("File.txt");
int index = Array.FindIndex(lines, line => line.StartsWith(nameToFind));
lines[index] = string.Join(" ", nameToFind, newValue1, newValue2);
File.WriteAllLines("File.txt", lines);
}
如果您只需要在行中設定兩個數字中的一個,您可以使用某種bool標志(editFirstNumber在下面的示例中)來定義應編輯的行中的第一個或第二個數字。要獲得這兩個數字,您可以" "使用string.Split()方法按空格分隔符分割行。
僅編輯兩個數字之一的示例:
static void EditValue(string nameToFind, int newValue, bool editFirstNumber)
{
string file = @"S:\File.txt";
if (File.Exists(file))
{
string[] lines = File.ReadAllLines(file);
for (int i = 0; i < lines.Length; i )
if (lines[i].StartsWith(nameToFind))
{
string[] lineValues = lines[i].Split(new char[] { ' ' }, 3, StringSplitOptions.None);
// Parse numbers from line
int.TryParse(lineValues[1], out int number1); // First number
int.TryParse(lineValues[2], out int number2); // Second number
// By boolean flag you can manipulate which one of two numbers to edit
if (editFirstNumber)
number1 = newValue;
else
number2 = newValue;
lines[i] = string.Join(" ", nameToFind, number1, number2);
// Optionally, if there is no more values to edit - you can break loop immediatly
break;
}
File.WriteAllLines(file, lines);
}
}
然后,您可以呼叫EditValues("Mattew", 6, 4);where 指定要查找的名稱以及第一個和第二個整數的新值。或者打電話EditValue("Mattew", 6, true);只編輯行中的第一個號碼并EditValues("Mattew", 4, false);編輯行中的第二個號碼。
如果您不能確定每一行都匹配“String Number1 Number2”方案,您可以在決議到int(例如!string.IsNullOrEmpty(lineValues[1]))之前添加一些檢查或使用int.TryParse(lineValues[1], out int currentValue1).
uj5u.com熱心網友回復:
您可以使用此功能查找和替換檔案中的行
public static bool lineChanger(string fileName, string name, int first_num, int sec_num)
{
//check for exist file
if (!File.Exists(fileName))
return false;
//find
var lines = File.ReadAllLines(fileName).ToList();
int index = lines.FindIndex(x => x.Substring(0, x.IndexOf(" ")).ToLower() == name.ToLower());
if (index == -1)
return false;
//edit line
var record = lines[index].Split(" ");
lines[index] = $"{record[0]} {Convert.ToInt32(record[1]) first_num} {Convert.ToInt32(record[2]) sec_num}";
File.WriteAllLines(fileName, lines);
return true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318459.html
下一篇:C-字陣列不斷被覆寫
