Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 123 32 40 14124 491 1
- OpenFileDialog ls = new OpenFileDialog();
- int numbersFromFile;
- if (ls.ShowDialog() == DialogResult.OK)
- {
- StreamReader read = new StreamReader(ls.FileName);
- }
- string lines = File.ReadAllText(ls.FileName);
- string[] words = lines.Split(new[] {' ', 'n'}, StringSplitOptions.RemoveEmptyEntries);
- string result = String.Join(" ", words.Where(w => w.Length < 3));
- // result == "32 40 1" given above input
- using (var reader=new System.IO.StreamReader("filepath")) // resources clean up
- {
- var text=reader.ReadToEnd().Split(' ').Where(c=> c.Length<=2).ToArray();
- // text is an array of strings that contains the numbers
- }
- string numbers = string.Empty;
- using (var reader = new StreamReader(@"C:Tempso.txt"))
- {
- numbers = reader.ReadToEnd();
- }
- var matches = numbers.Split(' ').Where(s => s.Length == 1 || s.Length == 2);
- foreach (var match in matches)
- {
- Console.WriteLine(match);
- }
- //create the Stream reader object
- StreamReader sr = new StreamReader("FilePath");
- //open and get the contents put into a string
- String documentText = sr.ReadToEnd();
- //close the reader
- sr.Close();
- //split out the text so we can look at each word
- String[] textStrings = documentText.Split(' ');
- //create a list to hold the results
- List<String> wordList = new List<String>();
- //loop through the words and check the length
- foreach (String word in textStrings)
- { //if it is less then 3 add to our list
- if (word.Length < 3)
- {
- wordList.Add(word);
- }
- }
- //...do what you need to with the results in the list
- foreach (String wordMatch in wordList)
- {
- MessageBox.Show("There are " + wordList.Count.ToString() + " items in the list");
- }
- string strRegex = @"(?<= )d{1,2}(?!d)";
- RegexOptions myRegexOptions = RegexOptions.Multiline;
- Regex myRegex = new Regex(strRegex, myRegexOptions);
- string strTargetString = @"123 32 40 14124 491 1"; // read.ReadToEnd();
- foreach (Match myMatch in myRegex.Matches(strTargetString))
- {
- if (myMatch.Success)
- {
- // Add your code here
- }
- }
Add Comment
Please, Sign In to add comment