Guest User

Untitled

a guest
Jan 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. 123 32 40 14124 491 1
  2.  
  3. OpenFileDialog ls = new OpenFileDialog();
  4. int numbersFromFile;
  5. if (ls.ShowDialog() == DialogResult.OK)
  6. {
  7. StreamReader read = new StreamReader(ls.FileName);
  8. }
  9.  
  10. string lines = File.ReadAllText(ls.FileName);
  11. string[] words = lines.Split(new[] {' ', 'n'}, StringSplitOptions.RemoveEmptyEntries);
  12. string result = String.Join(" ", words.Where(w => w.Length < 3));
  13.  
  14. // result == "32 40 1" given above input
  15.  
  16. using (var reader=new System.IO.StreamReader("filepath")) // resources clean up
  17. {
  18.  
  19. var text=reader.ReadToEnd().Split(' ').Where(c=> c.Length<=2).ToArray();
  20. // text is an array of strings that contains the numbers
  21. }
  22.  
  23. string numbers = string.Empty;
  24. using (var reader = new StreamReader(@"C:Tempso.txt"))
  25. {
  26. numbers = reader.ReadToEnd();
  27. }
  28.  
  29. var matches = numbers.Split(' ').Where(s => s.Length == 1 || s.Length == 2);
  30. foreach (var match in matches)
  31. {
  32. Console.WriteLine(match);
  33. }
  34.  
  35. //create the Stream reader object
  36. StreamReader sr = new StreamReader("FilePath");
  37. //open and get the contents put into a string
  38. String documentText = sr.ReadToEnd();
  39. //close the reader
  40. sr.Close();
  41. //split out the text so we can look at each word
  42. String[] textStrings = documentText.Split(' ');
  43. //create a list to hold the results
  44. List<String> wordList = new List<String>();
  45.  
  46. //loop through the words and check the length
  47. foreach (String word in textStrings)
  48. { //if it is less then 3 add to our list
  49. if (word.Length < 3)
  50. {
  51. wordList.Add(word);
  52. }
  53. }
  54.  
  55.  
  56. //...do what you need to with the results in the list
  57. foreach (String wordMatch in wordList)
  58. {
  59. MessageBox.Show("There are " + wordList.Count.ToString() + " items in the list");
  60. }
  61.  
  62. string strRegex = @"(?<= )d{1,2}(?!d)";
  63. RegexOptions myRegexOptions = RegexOptions.Multiline;
  64. Regex myRegex = new Regex(strRegex, myRegexOptions);
  65. string strTargetString = @"123 32 40 14124 491 1"; // read.ReadToEnd();
  66.  
  67. foreach (Match myMatch in myRegex.Matches(strTargetString))
  68. {
  69. if (myMatch.Success)
  70. {
  71. // Add your code here
  72. }
  73. }
Add Comment
Please, Sign In to add comment