C# - Read/Copy/Replace Lines In Text - c#
I have a text file that I am opening up and it is in a similar format to this:
10 SOME TEXT
20 T A40
B B5, C45, D48
30 B E25
40 B F17, G18
60 T H20, I23,
B J6, K7, L8, M9, N10, O11, P12,
Q31, R32, S33, T34, U35, V36,
W37, X38, Y39
100 T Z65
360 B A1, B4, C5, D6, E7, F10
2000 T SOME TEXT
423 TEXT
With this text I need to be able to read it and replace values accordingly. If a ReadLine begins with a number (ie, 10, 20, 30, 40, 60, 100, 360, 2000, 423) I need to to check if there is a T, B, or text after it. The only case that I need to change/reformat the lines when they come in and output them differently.
Example: 10 is fine except for I would like to add zeros in front of every number to make them 4 digits long (ie, 10 turns to 0010, 360 turns to 0360, 2000 stays the same). When the string "B B5, C45, D48" is read (this is the third line in the text) I need to change it to say "20A B5, C45, D48". I need to grab the number above the "B" and concat it to the "B" and replace the "B" with an "A". If instead of a "B" there is a "T" I simply need to remove the "T". Also, if a line does not start with a number or a "B" (ie, Q31 or W37) I need to concat that line with the previous line.
So after the changes take place it should look like this:
0010 SOME TEXT
0020 A40
0020A B5, C45, D48
0030A E25
0040A F17, G18
0060 H20, I23,
0060A J6, K7, L8, M9, N10, O11, P12, Q31, R32, S33, T34, U35, V36, W37, X38, Y39
0100 Z65
0360A A1, B4, C5, D6, E7, F10
2000 SOME TEXT
0423 TEXT
I am currently trying to use Regex to do this but I have been told that there is an easier way to do this and I am not sure how. So far I have been able to add the zeros in front of the numbers. Also, my code is adding an "A" to the end of everything as well as keeping the original number on the next line and I am not grabbing the lines that begin with anything but a digit.
This is what my current output is turning out to look like:
0010A
0010
0020A
0020
0030A
0030
0060A
0060
0100A
0100
0360A
0360
2000
2000
0423A
0423
I am obviously doing something wrong using Regex.
Here is my current code:
private void openRefsButton_Click(object sender, EventArgs e)
{
// Initialize the OpenFileDialog to specify the .txt extension as well as
// its intial directory for the file.
openRefs.DefaultExt = "*.txt";
openRefs.Filter = ".txt Files|*.txt";
openRefs.InitialDirectory = "C:\\";
openRefs.RestoreDirectory = true;
try
{
// Open the contents of the file into the originalTextRichTextBox.
if (openRefs.ShowDialog() == DialogResult.OK && openRefs.FileName.Length > 0)
refsTextRichTextBox.LoadFile(openRefs.FileName, RichTextBoxStreamType.PlainText);
// Throws a FileNotFoundException otherwise.
else
throw new FileNotFoundException();
StreamReader refsInput = File.OpenText(openRefs.FileName);
string regExpression = #"^[\d]+";
string findNewBottomRegex = #"^B\s";
StringBuilder buildNumberText = new StringBuilder();
StringBuilder formatMatchText = new StringBuilder();
foreach (string allLines in File.ReadAllLines(openRefs.FileName))
{
Match newBottomMatch = Regex.Match(allLines, findNewBottomRegex);
Match numberStartMatch = Regex.Match(allLines, regExpression);
int counter = 0;
if (counter < numberStartMatch.Length)
{
if (numberStartMatch.Value.Length == 2)
{
if (refsTextRichTextBox.Text.Contains(newBottomMatch.ToString()))
{
finalTextRichTextBox.AppendText("00" + numberStartMatch + "A\n");
}
finalTextRichTextBox.AppendText("00" + numberStartMatch + "\n");
}
else if (numberStartMatch.Value.Length == 3)
{
if (refsTextRichTextBox.Text.Contains(newBottomMatch.ToString()))
{
finalTextRichTextBox.AppendText("0" + numberStartMatch + "A\n");
}
finalTextRichTextBox.AppendText("0" + numberStartMatch + "\n");
}
else
{
if (refsTextRichTextBox.Text.Contains(newBottomMatch.ToString()))
{
finalTextRichTextBox.AppendText(numberStartMatch + "A\n");
}
finalTextRichTextBox.AppendText(numberStartMatch + "\n");
}
counter++;
}
}
}
// Catches an exception if the file was not opened.
catch (Exception)
{
MessageBox.Show("There was not a specified file path.", "Path Not Found Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
QUESTION(S):
What is a better way to go about doing this task?
Are there any recommendations on changing my code to be more efficient and cleaner?
How do I properly split each line into number, T/B, A40 when every line is not the same?
After the lines are properly split, how do I replace copy the line before if the current line begins with a "B"?
If the line begins with "Q31" or similar, how do I add that current line to the end of the previous one?
Once this happens, is there a way to concat everything to create the speficied format above?
WORK FLOW #jaywayco
Open Text File
Read file line by line
Save each line in a list of strings
Split each string by ' '
Find each line that starts with a digit
Replace that digit to make it 4 digits in length
Check the following text after the digit to see if it is a "B ", "T ", or "SOME TEXT"
if "B " copy the line above
Add an "A" to the end of the digit
if "T " remove the "T "
if "SOME TEXT" do nothing
Find each line that starts with a "B "
Copy the digits on the line above and concat to the front of the "B "
Follow step 4.b.i
Find each line that starts with (or similar to) "Q31"
Concat this line to the end of the previous line
...?
Here's a really lame, procedural solution:
using System.IO;
using System.Collections.Generic;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var list = new List<string>();
using (var reader = File.OpenText(#"c:\input.txt"))
{
while (true)
{
var line = reader.ReadLine();
if (string.IsNullOrEmpty(line)) break;
list.Add(line);
}
}
list = HandleRemoveTRequirement(list);
list = HandleFourDigitRequirement(list);
list = HandleConcatRequirement(list);
list = HandleStartsWithBRequirement(list);
list = HandleSecondElementIsBRequirement(list);
using (var output = new StreamWriter(#"c:\output.txt"))
{
foreach (var line in list)
{
output.WriteLine(line);
}
}
}
static List<string> HandleSecondElementIsBRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
if (parts[1].Equals("B"))
{
parts[0] += "A";
parts[1] = string.Empty;
result.Add(string.Join(" ", parts).Replace(" ", " "));
}
else
{
result.Add(line);
}
}
return result;
}
static List<string> HandleStartsWithBRequirement(List<string> list)
{
var result = new List<string>();
var i = 0;
foreach (var line in list)
{
var parts = line.Split(' ');
if (parts[0].Equals("B"))
{
parts[0] = string.Empty;
result.Add(list[i - 1].Split(' ')[0] + "A" + string.Join(" ", parts));
}
else
{
result.Add(line);
}
i++;
}
return result;
}
static List<string> HandleConcatRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
int test;
if (int.TryParse(parts[0], out test) || parts[0].Equals("B"))
{
result.Add(line);
}
else
{
result[result.Count -1] += line;
}
}
return result;
}
static List<string> HandleRemoveTRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
if (parts[1].Equals("T"))
{
parts[1] = string.Empty;
}
result.Add(string.Join(" ", parts).Replace(" ", " "));
}
return result;
}
static List<string> HandleFourDigitRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
int test;
if (int.TryParse(parts[0], out test))
{
parts[0] = parts[0].PadLeft(4, '0');
result.Add(string.Join(" ", parts));
}
else
{
result.Add(line);
}
}
return result;
}
}
}
These are pretty complicated requirements and I would be tempted to implement this as a workflow. This way you can separate out each of the logical steps and this will increase maintainability.
I would be tempted to represent the text file as an array of string arrays or even a data table. Then you can write general functions that concatenate/transform specific values
One way to possibly approach this is similiar to jaywayco's.
I'd start with placing each line split by spaces into it's own array. Place that array into an Array of arrays. From there you can consider your workflow. Your line array that is split by the spaces you can determine how to print it based off the first value, being a number or letter B etc... If it's a B, you know that it should start with array[i-1] first value, which would be the number etc. You'd have to think through the logic a bit, but I think you can understand where I am coming from. I'm not sure if this is the best approach or not, but I think this is the way I would tackle it. Good luck!
Edit: Here is some mock code...
var mainArray = new Array[textFile.Count];
//obviously get the count of number of lines set that to the size of your array object.
for(int i=0; i < mainArray.Length; i++)
{
var line = methodToGetLineFromTextFile[i];
string[] lineArray = line.Split(' ');
mainArray[i] = lineArray;
}
//Once you have everything loaded into your arrays, apply your workflow logic.
Hope this helps!
The way I would go about this task is to write a set of unit tests based on your requirements, then make them pass one at a time (having one test per requirement).
As jaywayco suggested, I would read the file into an array of lines, then implement each of your rules as a line transformation method which can be tested in isolation. I would probably separate out the method which can select which transformation(s) to apply. Then loop over the lines and apply the transformations.
Related
Remove preceeding CRLF if the current line starts with a SPACE
An example source I need to work with text file: Analysis: this is a test that wraps and this is the second line this is the third line Demonstration: This is an example of only one line Result data more text My expected output if all works: LineID Source Description 1 Analysis: this is a test that wraps and this is the second line this is the third line 2 Demonstration: This is an example of only one line 3 Result data more text This is my code that I want to add to the datagridview string path = #"Sample1.txt"; dtSource.Columns.Add("LineID"); dtSource.Columns.Add("Source"); dtSource.Columns.Add("Description"); string[] readText = File.ReadAllLines(path); int linenum = 0; foreach (string s in readText) { string sourceline = s; if(sourceline.StartsWith(" ")) { sourceline = sourceline.Replace("\n", "").Replace("\r", ""); //STUCK } dtSource.Rows.Add(linenum, sourceline, ""); linenum++; } dataGridView1.DataSource = dtSource; Now I can't clearly think. Was thinking to just use a List<string> then manipulate but didn't want to add more stuff. Also, thought about just modifying the text from the previous datatable row joining the two. Any linq queries or something that I can do to simplify this?
This should work foreach (string s in readText) { string sourceline = s; if (sourceline.StartsWith(" ")) { dtSource.Rows[linenum - 1]["Source"] += sourceline; } else { dtSource.Rows.Add(linenum, sourceline, ""); linenum++; } }
You can use Aggregate var result = readText.Aggregate(new List<string>(), (acc, x) => { if (x.StartsWith(" ")) { acc[acc.Count - 1] += x; } else { acc.Add(x); } return acc; }).Select((s, i) => dtSource.Rows.Add(i + 1, s, string.Empty)).ToList();
Going to the end of a substring in c#
The comment // go to end, I can't figure out how to cleanly end the substring :( Is there a simpler way to go to the end of the substring rather than mathing out the number by myself? For more complex strings this would be too hard string word = Console.ReadLine(); string[] lines = File.ReadAllLines(file); using (var far = File.CreateText(resultfile)) { foreach (string line in lines) { StringBuilder NewL = new StringBuilder(); int ind = line.IndexOf(word); if (ind >= 0) { if (ind == 0) { NewL.Append(line.Substring(ind+ word.Length +1, // go to end); }else{ NewL.Append(line.Substring(0, ind - 1)); NewL.Append(line.Substring(ind + word.Length + 1, // go to end));} far.WriteLine(NewL); } else { far.WriteLine(line); } } I don't know what more details the stackoverflow wants, anyone who can answer this pretty sure can clearly understand this simple code anyways.
You can use the String.Substring(int) overload, which automatically continues to the end of the source string: NewL.Append(line.Substring(ind + word.Length + 1)); Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
It seems to me that you are just trying to remove a certain word from the loaded lines. If this is your task then you can simply replace the word with an empty string foreach (string line in lines) { string newLine = line.Replace(word, ""); far.WriteLine(newLine); } Or even without an explicit loop with a bit of Linq var result = lines.Select(x = x.Replace(word,"")); File.WriteAllLines("yourFile.txt", result); Or, given the requirement to match an additional character after the word you can solve it with Regex. Regex r = new Regex(word + "."); var result = lines.Select(x => r.Replace(x, "")); File.WriteAllLines("yourFile.txt", result);
Replacing word in different ways
For first I have phrase like ice z comp. In the output I need List<string> which have this word with dashes for example like this: ice-z comp, ice z-comp, ice-z-comp For now I have this: var synonymFromSynonym = new List<string>(); var countOfSpaces = word.Count(Char.IsWhiteSpace); for (int x = 0; x < countOfSpaces; x++) { // What here for my output ? }
The problem can be solved using the following implementation. First, the input is split into the individual words. var Words = word.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToArray(); Then, the words are assembled again, considering two cases, namely whether the connection is done with a space or with a hyphen. This can be done recursively as follows, where a call like GenerateResult("", 0 ) would start the calculation. List<string> Result = new List<string>(); void GenerateResult(string Intermediate, int Position) { if (Position == Words.Count() - 1) // base case, nothing to append { Result.Add(Intermadiate); } else { GenerateResult( Intermediate + " ", Position + 1 ); // space case GenerateResult( Intermediate + "-", Position + 1 ); // hyphen case } }
Alternatively upper- and lowercase words in a string
I use Visual Studio 2010 ver. I have array strings [] = { "eat and go"}; I display it with foreach I wanna convert strings like this : EAT and GO Here my code: Console.Write( myString.First().ToString().ToUpper() + String.Join("",myString].Skip(1)).ToLower()+ "\n"); But the output is : Eat and go . :D lol Could you help me? I would appreciate it. Thanks
While .ToUpper() will convert a string to its upper case equivalent, calling .First() on a string object actually returns the first element of the string (since it's effectively a char[] under the hood). First() is actually exposed as a LINQ extension method and works on any collection type. As with many string handling functions, there are a number of ways to handle it, and this is my approach. Obviously you'll need to validate value to ensure it's being given a long enough string. using System.Text; public string CapitalizeFirstAndLast(string value) { string[] words = value.Split(' '); // break into individual words StringBuilder result = new StringBuilder(); // Add the first word capitalized result.Append(words[0].ToUpper()); // Add everything else for (int i = 1; i < words.Length - 1; i++) result.Append(words[i]); // Add the last word capitalized result.Append(words[words.Length - 1].ToUpper()); return result.ToString(); }
If it's always gonna be a 3 words string, the you can simply do it like this: string[] mystring = {"eat and go", "fast and slow"}; foreach (var s in mystring) { string[] toUpperLower = s.Split(' '); Console.Write(toUpperLower.First().ToUpper() + " " + toUpperLower[1].ToLower() +" " + toUpperLower.Last().ToUpper()); }
If you want to continuously alternate, you can do the following: private static string alternateCase( string phrase ) { String[] words = phrase.split(" "); StringBuilder builder = new StringBuilder(); //create a flag that keeps track of the case change book upperToggle = true; //loops through the words for(into i = 0; i < words.length; i++) { if(upperToggle) //converts to upper if flag is true words[i] = words[i].ToUpper(); else //converts to lower if flag is false words[i] = words[i].ToLower(); upperToggle = !upperToggle; //adds the words to the string builder builder.append(words[i]); } //returns the new string return builder.ToString(); }
Quickie using ScriptCS: scriptcs (ctrl-c to exit) > var input = "Eat and go"; > var words = input.Split(' '); > var result = string.Join(" ", words.Select((s, i) => i % 2 == 0 ? s.ToUpperInvariant() : s.ToLowerInvariant())); > result "EAT and GO"
SubString editing
I've tried a few different methods and none of them work correctly so I'm just looking for someone to straight out show me how to do it . I want my application to read in a file based on an OpenFileDialog. When the file is read in I want to go through it and and run this function which uses Linq to insert the data into my DB. objSqlCommands.sqlCommandInsertorUpdate However I want to go through the string , counting the number of ","'s found . when the number reaches four I want to only take the characters encountered until the next "," and do this until the end of the file .. can someone show me how to do this ? Based on the answers given here my code now looks like this string fileText = File.ReadAllText(ofd.FileName).Replace(Environment.NewLine, ","); int counter = 0; int idx = 0; List<string> foo = new List<string>(); foreach (char c in fileText.ToArray()) { idx++; if (c == ',') { counter++; } if (counter == 4) { string x = fileText.Substring(idx); foo.Add(fileText.Substring(idx, x.IndexOf(','))); counter = 0; } } foreach (string s in foo) { objSqlCommands.sqlCommandInsertorUpdate("INSERT", s);//laClient[0]); } However I am getting an "length cannot be less than 0" error on the foo.add function call , any ideas ?
A Somewhat hacky example. You would pass this the entire text from your file as a single string. string str = "1,2,3,4,i am some text,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20"; int counter = 0; int idx = 0; List<string> foo = new List<string>(); foreach (char c in str.ToArray()) { idx++; if (c == ',') { counter++; } if (counter == 4) { string x = str.Substring(idx); foo.Add(str.Substring(idx, x.IndexOf(','))); counter = 0; } } foreach(string s in foo) { Console.WriteLine(s); } Console.Read(); Prints: i am some text 9 13 17
As Raidri indicates in his answer, String.Split is definitely your friend. To catch every fifth word, you could try something like this (not tested): string fileText = File.ReadAllText(OpenDialog.FileName).Replace(Environment.NewLine, ","); string words[] = fileText.Split(','); List<string> everFifthWord = new List<string>(); for (int i = 4; i <= words.Length - 1, i + 5) { everyFifthWord.Add(words[i]); } The above code reads the selected file from the OpenFileDialog, then replaces every newline with a ",". Then it splits the string on ",", and starting with the fifth word takes every fifth word in the string and adds it to the list.
File.ReadAllText reads a text file to a string and Split turns that string into an array seperated at the commas: File.ReadAllText(OpenDialog.FileName).Split(',')[4] If you have more than one line use: File.ReadAllLines(OpenDialog.FileName).Select(l => l.Split(',')[4]) This gives an IEnumerable<string> where each string contains the wanted part from one line of the file
It's not clear to me if you're after every fifth piece of text between the commas or if there are multiple lines and you want only the fifth piece of text on each line. So I've done both. Every fifth piece of text: var text = "1,2,3,4,i am some text,6,7,8,9" + ",10,11,12,13,14,15,16,17,18,19,20"; var everyFifth = text .Split(',') .Where((x, n) => n % 5 == 4); Only the fifth piece of text on each line: var lines = new [] { "1,2,3,4,i am some text,6,7", "8,9,10,11,12,13,14,15", "16,17,18,19,20", }; var fifthOnEachLine = lines .Select(x => x.Split(',')[4]);