You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use this console app generated by GitHub Copilot to convert to interpolated strings:
usingSystem.Text.RegularExpressions;classProgram{staticvoidMain(){stringdirectory="c:\\repos\\docs\\docs\\fundamentals\\runtime-libraries";if(!Directory.Exists(directory)){Console.WriteLine($"Directory '{directory}' does not exist.");return;}foreach(stringfileinDirectory.GetFiles(directory,"*.cs",SearchOption.AllDirectories)){ConvertFileToInterpolatedStrings(file);}}staticvoidConvertFileToInterpolatedStrings(stringfilePath){stringfileContent=File.ReadAllText(filePath);stringupdatedContent=ConvertToInterpolatedStrings(fileContent);if(fileContent!=updatedContent){File.WriteAllText(filePath,updatedContent);Console.WriteLine($"Updated file: {filePath}");}}staticstringConvertToInterpolatedStrings(stringcontent){stringpattern=@"Console\.WriteLine\(([^;]*)\);";returnRegex.Replace(content,pattern, match =>{stringarguments=match.Groups[1].Value;string[]parts=SplitArgument(arguments);if(parts.Length<2)returnmatch.Value;stringformatString=parts[0].Trim();string[]formatArguments=parts[1..].Select(arg =>arg.Trim()).ToArray();if(!formatString.StartsWith('"')||!formatString.EndsWith('"'))returnmatch.Value;formatString=formatString[1..^1];// Remove quotesstringinterpolatedString="Console.WriteLine($\""+Regex.Replace(formatString,@"\{(\d+)(,[^}]*)?(:[^}]*)?\}", m =>{intindex=int.Parse(m.Groups[1].Value);stringalignment=m.Groups[2].Value;stringformatSpecifier=m.Groups[3].Value;if(index<formatArguments.Length){return"{"+formatArguments[index].Trim()+alignment+formatSpecifier+"}";}returnm.Value;})+"\");";returninterpolatedString;});}staticstring[]SplitArgument(stringargument){varparts=newList<string>();intbracketCount=0;intstart=0;for(inti=0;i<argument.Length;i++){if(argument[i]==','&&bracketCount==0){parts.Add(argument[start..i].Trim());start=i+1;}elseif(argument[i]=='('){bracketCount++;}elseif(argument[i]==')'){bracketCount--;}}parts.Add(argument[start..].Trim());return[.. parts];}}
The text was updated successfully, but these errors were encountered:
Use this console app generated by GitHub Copilot to convert to interpolated strings:
The text was updated successfully, but these errors were encountered: