Bobby
Bobby
CC#
Created by Thommy on 11/17/2023 in #help
String Append
@Thommy
public static void Main()
{
string filePath = "path/to/your/file.txt";

try
{
string[] blablaContent = File.ReadAllLines(filePath);


string updatedContent = FindAndInsert(blablaContent, "blablabla.2", "row2.withCode","what ever text you want" )
// Write the updated content back to the file
File.WriteAllText(filePath, updatedContent);

Console.WriteLine("File content updated successfully.");
}
catch (IOException e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}

public static string FindAndInsert(string[] text, string searchSegmentText,string searchRowText, string insertText)
{
List<string> list = new List<string>();
bool foundSegment = false;
foreach (var line in text)
{
list.Add(line);
var lnText = line.Replace("{","").Trim();
if (lnText.Equals(searchSegmentText))
{
foundSegment = true;
}
if (foundSegment && line.Trim().Equals(searchRowText))
{
list.Add(insertText);
}
}

return string.Join("\n", list);

}
public static void Main()
{
string filePath = "path/to/your/file.txt";

try
{
string[] blablaContent = File.ReadAllLines(filePath);


string updatedContent = FindAndInsert(blablaContent, "blablabla.2", "row2.withCode","what ever text you want" )
// Write the updated content back to the file
File.WriteAllText(filePath, updatedContent);

Console.WriteLine("File content updated successfully.");
}
catch (IOException e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}

public static string FindAndInsert(string[] text, string searchSegmentText,string searchRowText, string insertText)
{
List<string> list = new List<string>();
bool foundSegment = false;
foreach (var line in text)
{
list.Add(line);
var lnText = line.Replace("{","").Trim();
if (lnText.Equals(searchSegmentText))
{
foundSegment = true;
}
if (foundSegment && line.Trim().Equals(searchRowText))
{
list.Add(insertText);
}
}

return string.Join("\n", list);

}
I hope this helps ...
7 replies