Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. String.IndexOf()
  2.  
  3. String.IndexOf()
  4.  
  5. public List<string> ParseWords(string s)
  6. {
  7. List<string> words = new List<string>();
  8.  
  9. int pos = 0;
  10. while (pos < s.Length)
  11. {
  12. // Get word start
  13. int start = pos;
  14.  
  15. // Get word end
  16. pos = s.IndexOf('|', pos);
  17. while (pos > 0 && s[pos - 1] == '\')
  18. {
  19. pos++;
  20. pos = s.IndexOf('|', pos);
  21. }
  22.  
  23. // Adjust for pipe not found
  24. if (pos < 0)
  25. pos = s.Length;
  26.  
  27. // Extract this word
  28. words.Add(s.Substring(start, pos - start));
  29.  
  30. // Skip over pipe
  31. if (pos < s.Length)
  32. pos++;
  33. }
  34. return words;
  35. }
  36.  
  37. string test = @"This|is|a|pip|ed|test (this is a pip|ed test)";
  38. string[] parts = Regex.Split(test, @"(?<!(?<!\)*\)|");
  39.  
  40. string sPipeSplit = "This|is|a|pip\|ed|test (this is a pip|ed test)";
  41. string sTempString = sPipeSplit.Replace("\|", "¬"); //replace | with non printable character
  42. string[] sSplitString = sTempString.Split('|');
  43. //string sFirstString = sSplitString[0].Replace("¬", "\|"); //If you have fixed number of fields and you are copying to other field use replace while copying to other field.
  44. /* Or you could use a loop to replace everything at once
  45. foreach (string si in sSplitString)
  46. {
  47. si.Replace("¬", "\|");
  48. }
  49. */
  50.  
  51. string text = @"This|is|a|pip|ed|test"; //The original text
  52. string parsed = ""; //Where you will store the parsed string
  53.  
  54. bool flag = false;
  55. foreach (var x in text.Split('|')) {
  56. bool endsWithArroba = x.EndsWith(@"");
  57. parsed += flag ? "|" + x + " " : endsWithArroba ? x.Substring(0, x.Length-1) : x + " ";
  58. flag = endsWithArroba;
  59. }
  60.  
  61. /^(?:((?:[^|\]|(?:\{2})|\|)+)(?:||$))*/
  62.  
  63. ^ #The start of a line
  64. (?:...
  65. [^|\] #A character other than | or OR
  66. (?:\{2})* #An even number of characters OR
  67. \| #A literal followed by a literal |
  68. ...)+ #Repeat the preceding at least once
  69. (?:$||) #Either a literal | or the end of a line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement