There are no new
statements in your code so you cannot switch to the new initializer syntax if that is what you're asking about.
Your code is actually just in need of some simple refactoring. The most complex aspect is your various logic to figure out FY vs quarter and A vs E and since ultimately it just impacts the output I think you can add a simple RE to capture all this to avoid the string manipulation. Perhaps something like this.
var lstperiods = periods.Split(',');
//Guessing at the logic here but it appears your periods are formatted like 2021A or FY2022 in which case we can simplify this logic to parse out the info
//Breaking this up from the Split just so you can see the code, can be combined
var items = from p in lstperiods
let info = PeriodInfo.Parse(p)
where info != null
select new { Period = p, Info = info };
//Another issue is that you appear to be using the same cell each time as you're always using [7, Counter] so only the last item really matters...
foreach (var item in items)
{
ws.Cells[7, Counter].Value = $"{(item.Info.IsFiscalYear ? "FY" : "")}{item.Info.Year}{item.Info.Category}";
};
This relies on a helper type to handle the string parsing and I'm just guessing at your string format.
public class PeriodInfo
{
public static PeriodInfo? Parse ( string value )
{
var match = s_regex.Match(value);
if (!match.Success)
return null;
if (!Int32.TryParse(match.Groups["year"].Value, out var year))
return null;
return new PeriodInfo() {
Year = year,
Category = match.Groups["category"].Value,
Quarter = match.Groups["quarter"].Value
};
}
public int? Year { get; set; }
public string Quarter { get; set; }
public bool IsFiscalYear => String.Equals(Quarter, "FY", StringComparison.OrdinalIgnoreCase);
public string Category { get; set; }
//Format this RE to fit your format - (A | E)yy(#Q | FY)
private static Regex s_regex = new Regex(@"(?<category>A | E)(?<year>\d{2})(?<quarter>\dQ | FY)",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
}