fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. var s = "=StringOne&=StringTwo&=StringThree&=StringFour&";
  12. var idx_to_replace = 2; // Replace this occurrence
  13. var cnt = 0; // Counter
  14. var result = string.Empty; // Final result variable
  15. var rx = "[^=]+(?=&)"; // Pattern
  16. for (var m=Regex.Match(s, rx); m.Success; m = m.NextMatch())
  17. {
  18. cnt++;
  19. if (cnt == idx_to_replace) {
  20. result = $"{s.Substring(0, m.Index)}REPLACED{s.Substring(m.Index+m.Length)}";
  21. break;
  22. }
  23. }
  24. Console.WriteLine(result);
  25. }
  26. }
Success #stdin #stdout 0.04s 134720KB
stdin
Standard input is empty
stdout
=StringOne&=REPLACED&=StringThree&=StringFour&