I want to replace a specific word in my string list in c#. I created a list that contains appointmens like([9.9.2009] meeting at 9:00), and now i want to add a function to edit the appointments like "[9.9.2009]" for "[8.8.2008]".
This is my code:
int count = File.ReadLines(appointmentPath + "appointment.txt").Count();
Console.WriteLine("Which appointment you want to edit? (1 - " + count + ")");
if (count != 0)
{
for (int i = 1; i < count + 1; i++)
{
List<string> linescount = File.ReadAllLines(appointmentPath + "appointment.txt").ToList();
Console.WriteLine(i + ". " + linescount[i - 1]);
}
int input = Convert.ToInt32(Console.ReadLine());
List<string> linesList = File.ReadAllLines(appointmentPath + "appointment.txt").ToList();
Console.Clear();
if(input != 1)
{
Console.WriteLine(linesList[input - 1]);
}
else
{
Console.WriteLine(linesList[0]);
}
Console.WriteLine("What you want to edit? (1. Date, 2. Summary, 3. Time)");
input = Convert.ToInt32(Console.ReadLine());
switch (input)
{
case 1: break;
case 2: break;
case 3: break;
default: Console.WriteLine("Input invalid"); break;
}
I suggest using regular expressions here: we can match (and replace) all fragments like ( d
here is a digit)
d.d.dddd
d.dd.dddd
dd.d.dddd
dd.dd.dddd
We can do it with a help of pattern
[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}
which means
[0-9]{1,2} - 1 or 2 digits (symbols in 0..9 range)
\. - dot .
[0-9]{1,2} - 1 or 2 digits (symbols in 0..9 range)
\. - dot .
[0-9]{4} - 4 digits (symbols in 0..9 range)
Code:
string result = Regex.Replace(
text,
@"[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}",
"8.8.2008");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.