Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. myString.Replace ('c', '')
  2.  
  3. myString.Replace ('c', '')
  4.  
  5. myString.Replace ("c", "")
  6.  
  7. char? c
  8.  
  9. public static class StringExtensions
  10. {
  11. public static IEnumerable<char> RemoveChar(this IEnumerable<char> originalString, char removingChar)
  12. {
  13. return originalString.Where(@char => @char != removingChar);
  14. }
  15. }
  16.  
  17. string veryLongText = "abcdefghijk...";
  18.  
  19. IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
  20. .RemoveChar('c')
  21. .RemoveChar('d')
  22. .Take(5);
  23.  
  24. public static class StringExtensions
  25. {
  26. public static IEnumerable<char> RemoveChars(this IEnumerable<char> originalString,
  27. params char[] removingChars)
  28. {
  29. return originalString.Except(removingChars);
  30. }
  31. }
  32.  
  33. var veryLongText = "abcdefghijk...";
  34. IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
  35. .RemoveChars('c', 'd')
  36. .Take(5)
  37. .ToArray(); //to prevent multiple execution of "RemoveChars"
  38.  
  39. default(char)
  40.  
  41. myString = myString.Replace('c'.ToString(), "");
  42.  
  43. myString = myString.Replace('t'.ToString(), "");
  44.  
  45. myString.Replace("c", "")
  46.  
  47. private void button1_Click(object sender, EventArgs e)
  48. {
  49.  
  50. Int32 i;
  51.  
  52. String name;
  53.  
  54. Int32[] array_number = new int[100];
  55.  
  56. name = "1 3 5 17 8 9 6";
  57.  
  58. name = name.Replace(' ', 'x');
  59.  
  60. char[] chr = name.ToCharArray();
  61.  
  62.  
  63. for (i = 0; i < name.Length; i++)
  64. {
  65. if ((chr[i] != 'x'))
  66. {
  67. array_number[i] = Convert.ToInt32(chr[i].ToString());
  68. MessageBox.Show(array_number[i].ToString());
  69. }
  70.  
  71. }
  72.  
  73. }
  74.  
  75. char? myCharFromUI = Convert.ToChar(" ");
  76. string myStringForDatabaseInsert = myCharFromUI.ToString().Trim();
  77. if (String.IsNullOrEmpty(myStringForDatabaseInsert.Trim()))
  78. {
  79. Console.Write("Success");
  80. }
  81.  
  82. // LinqPad .ReplaceAll and SafeFileName
  83. void Main()
  84. {
  85.  
  86. ("a:B:C").Replace(":", "_").Dump(); // can only replace 1 character for one character => a_B_C
  87. ("a:B:C").Replace(":", null).Dump(); // null replaces with empty => aBC
  88. ("a:B*C").Replace(":", null).Replace("*",null).Dump(); // Have to chain for multiples
  89.  
  90. // Need a ReplaceAll, so I don't have to chain calls
  91.  
  92.  
  93. ("abc/123.txt").SafeFileName().Dump();
  94. ("abc/1/2/3.txt").SafeFileName().Dump();
  95. ("a:bc/1/2/3.txt").SafeFileName().Dump();
  96. ("a:bc/1/2/3.txt").SafeFileName('_').Dump();
  97. //("abc/123").SafeFileName(':').Dump(); // Throws exception as expected
  98.  
  99. }
  100.  
  101.  
  102. static class StringExtensions
  103. {
  104.  
  105. public static string SafeFileName(this string value, char? replacement = null)
  106. {
  107. return value.ReplaceAll(replacement, ':','*','?','"','<','>', '|', '/', '\');
  108. }
  109.  
  110. public static string ReplaceAll(this string value, char? replacement, params char[] charsToGo){
  111.  
  112. if(replacement.HasValue == false){
  113. return string.Join("", value.AsEnumerable().Where(x => charsToGo.Contains(x) == false));
  114. }
  115. else{
  116.  
  117. if(charsToGo.Contains(replacement.Value)){
  118. throw new ArgumentException(string.Format("Replacement '{0}' is invalid. ", replacement), "replacement");
  119. }
  120.  
  121. return string.Join("", value.AsEnumerable().Select(x => charsToGo.Contains(x) == true ? replacement : x));
  122. }
  123.  
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement