Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Some clarifications

I got a file with lots of ASCII characters. [...] I have many characters like L¿ö in the file [...]

As others have pointed out, these are not ASCII characters.

No extension, and has the Type Of file attribute 190000 File.

Configure Windows Explorer to show file extensions.

I required to remove lines from a word PYGR to another word MCG. It has several lines in between.

Your code tells us otherwise. You are not removing the lines from PYGR to MCG; your code removes the word PYGR and every character that follows up to one character before MCG. I assume that is not quite what you were trying to achieve?

(削除) If you update your question to explain if you are trying to remove everything between (excluding) PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter. (削除ここまで)

Edit: All right, in accordance with your comment I have changed my answer to replace all text from PYGR (inclusive) to MCG (exclusive).

#Reviewing your code

Reviewing your code

Readability

  • Please use the well-established C# naming conventions: local variables are camelCase
  • Format your code with the correct indentation to make it more readable
  • Start your file with using System.IO to avoid repeating yourself
  • Get rid of unnecessary parenthesis
  • Choose proper names
  • Join declaration and usage of variables where possible

For example:

string EditedString;
// ...
EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1))); 

should be more like

string editedContent = fileContent.Remove(startIndex, endIndex - end.Length); 

Maintainability

Hard-coding the paths is something that should be avoided even during development. You can use Environment.GetFolderPath with Environment.SpecialFolder.Desktop to retrieve the path to the current user's desktop and add the file name with Path.Combine.

##Regex

Regex

I really don't like the heave use of indexOf: your code seems to be all about how the replacement is done, instead of what is going on (i.e. it is imperative, not declarative).

Using a Regex, we can arrive at a more declarative style. You may need to adjust this to suit your needs (your question is ambiguous).


#Refactored

Refactored

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1"); 
// you would probably pass the paths into your method as parameters
var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", "MCG", RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);

Some clarifications

I got a file with lots of ASCII characters. [...] I have many characters like L¿ö in the file [...]

As others have pointed out, these are not ASCII characters.

No extension, and has the Type Of file attribute 190000 File.

Configure Windows Explorer to show file extensions.

I required to remove lines from a word PYGR to another word MCG. It has several lines in between.

Your code tells us otherwise. You are not removing the lines from PYGR to MCG; your code removes the word PYGR and every character that follows up to one character before MCG. I assume that is not quite what you were trying to achieve?

(削除) If you update your question to explain if you are trying to remove everything between (excluding) PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter. (削除ここまで)

Edit: All right, in accordance with your comment I have changed my answer to replace all text from PYGR (inclusive) to MCG (exclusive).

#Reviewing your code

Readability

  • Please use the well-established C# naming conventions: local variables are camelCase
  • Format your code with the correct indentation to make it more readable
  • Start your file with using System.IO to avoid repeating yourself
  • Get rid of unnecessary parenthesis
  • Choose proper names
  • Join declaration and usage of variables where possible

For example:

string EditedString;
// ...
EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1))); 

should be more like

string editedContent = fileContent.Remove(startIndex, endIndex - end.Length); 

Maintainability

Hard-coding the paths is something that should be avoided even during development. You can use Environment.GetFolderPath with Environment.SpecialFolder.Desktop to retrieve the path to the current user's desktop and add the file name with Path.Combine.

##Regex

I really don't like the heave use of indexOf: your code seems to be all about how the replacement is done, instead of what is going on (i.e. it is imperative, not declarative).

Using a Regex, we can arrive at a more declarative style. You may need to adjust this to suit your needs (your question is ambiguous).


#Refactored

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1"); 
// you would probably pass the paths into your method as parameters
var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", "MCG", RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);

Some clarifications

I got a file with lots of ASCII characters. [...] I have many characters like L¿ö in the file [...]

As others have pointed out, these are not ASCII characters.

No extension, and has the Type Of file attribute 190000 File.

Configure Windows Explorer to show file extensions.

I required to remove lines from a word PYGR to another word MCG. It has several lines in between.

Your code tells us otherwise. You are not removing the lines from PYGR to MCG; your code removes the word PYGR and every character that follows up to one character before MCG. I assume that is not quite what you were trying to achieve?

(削除) If you update your question to explain if you are trying to remove everything between (excluding) PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter. (削除ここまで)

Edit: All right, in accordance with your comment I have changed my answer to replace all text from PYGR (inclusive) to MCG (exclusive).

Reviewing your code

Readability

  • Please use the well-established C# naming conventions: local variables are camelCase
  • Format your code with the correct indentation to make it more readable
  • Start your file with using System.IO to avoid repeating yourself
  • Get rid of unnecessary parenthesis
  • Choose proper names
  • Join declaration and usage of variables where possible

For example:

string EditedString;
// ...
EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1))); 

should be more like

string editedContent = fileContent.Remove(startIndex, endIndex - end.Length); 

Maintainability

Hard-coding the paths is something that should be avoided even during development. You can use Environment.GetFolderPath with Environment.SpecialFolder.Desktop to retrieve the path to the current user's desktop and add the file name with Path.Combine.

Regex

I really don't like the heave use of indexOf: your code seems to be all about how the replacement is done, instead of what is going on (i.e. it is imperative, not declarative).

Using a Regex, we can arrive at a more declarative style. You may need to adjust this to suit your needs (your question is ambiguous).


Refactored

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1"); 
// you would probably pass the paths into your method as parameters
var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", "MCG", RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);
added 149 characters in body
Source Link
Adam
  • 5.2k
  • 1
  • 30
  • 47

If you update(削除) If you update your question to explain if you are trying to remove everything between (excluding)PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter. (削除ここまで)

Edit: All right, in accordance with your question to explain if you are tryingcomment I have changed my answer to remove everything between (excluding)replace all text from PYGR and(inclusive) to MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter(exclusive).

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1"); 
// you would probably pass the paths into your method as parameters
var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", string.Empty"MCG", RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);

If you update your question to explain if you are trying to remove everything between (excluding) PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter.

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1"); 
// you would probably pass the paths into your method as parameters
var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", string.Empty, RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);

(削除) If you update your question to explain if you are trying to remove everything between (excluding)PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter. (削除ここまで)

Edit: All right, in accordance with your comment I have changed my answer to replace all text from PYGR (inclusive) to MCG (exclusive).

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1"); 
// you would probably pass the paths into your method as parameters
var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", "MCG", RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);
Source Link
Adam
  • 5.2k
  • 1
  • 30
  • 47

Some clarifications

I got a file with lots of ASCII characters. [...] I have many characters like L¿ö in the file [...]

As others have pointed out, these are not ASCII characters.

No extension, and has the Type Of file attribute 190000 File.

Configure Windows Explorer to show file extensions.

I required to remove lines from a word PYGR to another word MCG. It has several lines in between.

Your code tells us otherwise. You are not removing the lines from PYGR to MCG; your code removes the word PYGR and every character that follows up to one character before MCG. I assume that is not quite what you were trying to achieve?

If you update your question to explain if you are trying to remove everything between (excluding) PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter.

#Reviewing your code

Readability

  • Please use the well-established C# naming conventions: local variables are camelCase
  • Format your code with the correct indentation to make it more readable
  • Start your file with using System.IO to avoid repeating yourself
  • Get rid of unnecessary parenthesis
  • Choose proper names
  • Join declaration and usage of variables where possible

For example:

string EditedString;
// ...
EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1))); 

should be more like

string editedContent = fileContent.Remove(startIndex, endIndex - end.Length); 

Maintainability

Hard-coding the paths is something that should be avoided even during development. You can use Environment.GetFolderPath with Environment.SpecialFolder.Desktop to retrieve the path to the current user's desktop and add the file name with Path.Combine.

##Regex

I really don't like the heave use of indexOf: your code seems to be all about how the replacement is done, instead of what is going on (i.e. it is imperative, not declarative).

Using a Regex, we can arrive at a more declarative style. You may need to adjust this to suit your needs (your question is ambiguous).


#Refactored

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1"); 
// you would probably pass the paths into your method as parameters
var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", string.Empty, RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);
default

AltStyle によって変換されたページ (->オリジナル) /