Home > Software engineering >  Can I shorten this code with a loop in C#?
Can I shorten this code with a loop in C#?

Time:01-27

I have this code written in C# but looks kind of "bad" and I would like to shorten it somehow and keep it clean and simple. All this code works pretty fine but I want to know if there's any other way I can achieve the same thing.

EDIT: I forgot to mention that the firstLine has a bad date format attached with it, so it is like this: "This_is_my_first_line_20220126". So I split the string and then only join it with the corrected date. The problem is that I can never know how long the new string would be and I don't want to handle the code like this and go up to 100 parts.

Here's my code:

string correctDate = "26012022";
string[] lines = File.ReadAllLines("text.txt");
string firstLine = lines.FirstOrDefault();
//note: firstLine looks like this: This_is_my_first_line_20220126
string[] sub = firstLine.Split('_');
string name="";

if(sub.Length==2)
  name = sub[0] "_" sub[1] "_" correctDate;
else if(sub.Length==3)
  name = sub[0] "_" sub[1] "_" sub[2] "_"correctDate;
...
else if(sub.Length==20)
  name = sub[0] "_"  ... "_"   sub[19];

Now, my final name value should be "This_is_my_line_26012022" but I want it to depend on the length of the given string. So far I know that the maximum length would go up to 20 but I don't want my code to look like this. Can I shorten it somehow? Thank you in advance.

CodePudding user response:

you can find the LastIndexOf the underscore and drop the date by using Substring:

string firstLine = "This_is_my_first_line_20220126";
string correctDate = "26012022";
string correctString = firstLine.Substring(0, firstLine.LastIndexOf("_")   1)   correctDate;

CodePudding user response:

Still a little perplexed with the split aproach, but this a way to join back all elements

string name = string.Join("_", sub.Take(sub.Length - 1).Append(correctDate));

Or use the substring method (and no need of all that split & join)

name = firstLine.Substring(0, firstLine.LastIndexOf("_")  1)   correctDate;

CodePudding user response:

I forgot to mention that firstLine has a bad date format like "This_is_my_Line_20220125"

If you want to correct just the first line:

string correctDate = "26012022";
string[] lines = File.ReadAllLines("text.txt");
lines[0] = lines[0][..^8]   correctDate;         

[..^8] uses C# 9's "indices and ranges" feature, that allows for a more compact way of taking a substring. It means "from the start of the string, up to the index 8 back from the end of the string".

If you get a wiggly line and possibly a messages like "... is not available in C# version X" you can use the older syntax, which would be more like lines[0] = lines[0].Remove(lines[0].Length - 8) correctDate;

If you want to correct all lines:

string correctDate = "26012022";
string[] lines = File.ReadAllLines("text.txt");
for(int x = 0; x < lines.Length; x  )
  lines[x] = lines[x][..^8]   correctDate;

If the incorrect date isn't always 8 characters long, you can use LastIndexOf('_') to locate the last _, and snip it to that point

CodePudding user response:

.

string[] lines = File.ReadAllLines("text.txt");
string firstLine = lines.FirstOrDefault();
name = firstLine.Substring(0, firstLine.LastIndexOf("_")   1)     "_"   correctDate ; ```
  •  Tags:  
  • Related