Reputation: 3
This is my text file
john monday 500 sara monday 600 sunny monday 1200 john monday 500 sara monday 300 sunny monday 2200 john monday 400 sara monday 100 sunny monday 500 john monday 520 sara monday 600 sunny monday 10 john monday 990 sara monday 850 sunny monday 1000 john monday 300 sara monday 200 sunny monday
is there any way i can read data and save it in string which comes after the name of SARA for eg from the above text file i need only the numbers which comes after the name of SARA , SUBSTRING is not helping because i have to specify the location of numbers over there . Don't know how to deal with it , i've done google fo now like an hour or so
please don't laugh at my try :(
using (StreamReader readtext = new StreamReader("F:\\spokeo\\2.txt"))
{
string readMeText = readtext.ReadLine();
Console.WriteLine(readMeText);
int index1 = Convert.ToInt32(readMeText.IndexOf('SARA', 0));
Console.WriteLine("The Index Value of character 'SARA' " +
"with start index 0 is " + index1);
}
Upvotes: 0
Views: 528
Reputation: 1047
Regular expressions can help on this requirement. You can define a search pattern with regular expressions and the library will find all results matching the pattern. Below code finds the numbers coming after name "sara".
using System;
using System.Text.RegularExpressions;
namespace consoleapp
{
class Program
{
static void Main(string[] args)
{
string text = "john monday 500 sara monday 600 sunny monday 1200 john monday 500 sara monday 300 sunny monday 2200 john monday 400 sara monday 100 sunny monday 500 john monday 520 sara monday 600 sunny monday 10 john monday 990 sara monday 850 sunny monday 1000 john monday 300 sara monday 200 sunny monday";
var saraPattern = new Regex(@"sara\s\w+\s\d+");
var numberPattern = new Regex(@"[\d\.\,]+");
var matches = saraPattern.Matches(text);
foreach (Match match in matches)
{
var numbermatch = numberPattern.Match(match.Value);
var number = float.Parse(numbermatch.Value);
Console.WriteLine(number);
}
Console.ReadKey();
}
}
}
As for the explanations of patterns:
saraPattern (sara\s\w+\s\d+
) states that: find a character sequence which starts with "sara
" then followed by a space character (\s
) then followed by one or more word characters (\w+
) then followed by a backspace again (\s
) then followed by one or more decimal characters (\d+)
This pattern will find sequences like "sara monday 600"
numberPattern ([\d\.\,]+
) states that: find a character sequence that contains one or more of any of these characters: decimal (\d
) or comma (\,
) or dot (\.
)
This pattern will extract the numbers (e.g. 600) from previously found sequence (e.g. "sara monday 600")
You can examine regular expressions on wikipedia (https://en.wikipedia.org/wiki/Regular_expression) or you can follow a basic tutorial on this site: https://regexone.com/
Upvotes: 2
Reputation: 1579
You have a bug in your code.
This line of yours:
int index1 = Convert.ToInt32(readMeText.IndexOf('SARA', 0));
should be:
int index1 = Convert.ToInt32(readMeText.IndexOf("sara", 0));
Comments: 1. You need to use " (quotes) not ' (apostrophe) 2. You do not have "SARA" in your string but "sara" that is why you get index with -1 at the moment.
Please let me know if it helps you!
Upvotes: 0