Reputation: 1058
in asp.net C#
let's say i want to store only the word "Batu Pahat" to database from following sentence
Conditions for Batu Pahat, MY at 11:00 am MYT
or "Mersing" from the following sentence?
Conditions for Mersing, MY at 12:00 pm MYT
Any one knows any algoritham to get this text? please help :)
UPDATE
Example sentences
Conditions for Kangar, MY at 10:00 am MYT
Conditions for Batu Pahat, MY at 11:00 am MYT
Conditions for Mersing, MY at 11:30 am MYT
Conditions for Segamat, MY at 12:00 pm MYT
Conditions for Johor Bahru Perdana, MY at 11:00 am MYT
Conditions for Muar, MY at 03:00 am MYT
Extra Info
The time "11:00 am" is not fixed.
Upvotes: 0
Views: 161
Reputation: 4376
Or without using Regex (as an alternative to the other answer), and only if the structure is fixed.
string startIgnorePart = "Conditions for ";
string findBefore = ",";
string original = "Conditions for Mersing, MY at 11:00 am MYT";
string extracted = original.Substring(startIgnorePart.Length, original.IndexOf(findBefore) - startIgnorePart.Length);
Console.WriteLine(extracted);
Upvotes: 5
Reputation: 5551
Regex should be able to help you out. I haven't tested this, but something like this should work.
Regex regex = new Regex("Conditions for (.*),");
var v = regex.Match("Conditions for Kangar, MY at 11:00 am MYT");
Upvotes: 3