C# How to retrieve a certain text located within a string?
Does anyone know how to retrieve a short sentence from within the main string? Is it required to use regular expressions?
I am trying to retrieve the text "Thu Dec 9 05:12:42 2010" which is the time from the main string "LastWrite Time Thu Dec 9 05:12:42 2010 (UTC)" which is after "Time" and before "(UTC)".
I am also new to C# so sorry for the simple question.
May someones please advise or show some C# methods that may be used to retrieved the text? Than开发者_运维问答ks!
You can use a regular expression:
Match match = Regex.Match(text, @"Time (.*?)\(UTC\)");
You can then get the substring you want from match.Groups[1].Value
.
Look into using the System.String.Substring
and System.String.Length
methods.
var logTime = "LastWrite Time Thu Dec 9 05:12:42 2010 (UTC)";
logTime = logTime.Substring("LastWrite Time ".Length);
logTime = logTime.Substring(0, logTime.Length - " (UTC)".Length);
This should get you close to what you're looking for.
You could use regular expressions (regex) but they are hard to understand. Most people consider them difficult to create unless you are very used to them. In case you are familliar with regular expressions, you can study the Regex class that handles regex in C#.
However what you try to do is also possible using usual methods on the String class. Look into it and see what methods you can use.
Mainly your logic using the string class' methods would be to determine if you have some pattern in your sentences. Try to find common elements that surround the data/time string you want to extract (begin and end). Then use the Substring(...)
method. or other methods. It can be done in multiple ways.
Following code should give you what you want. It is not a flexible solution and will work only for this scenario:
String value = "LastWrite Time Thu Dec 9 05:12:42 2010 (UTC)";
String newValue = value.Replace("LastWrite Time", "").Replace("(UTC)", "").Trim();
For more flexibility, use Regular Expressions
.
try this out, If your text is in only in string variable, you can use SubString() method to retrieve the string. Ex:
String str = "LastWrite Time Thu Dec 9 05:12:42 2010 (UTC)"; str.substring(15,23);
will return you Thu Dec 9 05:12:42 2010 as result.
Your problem is essentially string parsing.
The easiest way is to just use the IndexOf and Substring methods on the string itself. This will only work for very simple scenarios. Just remember, the string is immutable and those functions will always return new strings, never modify the original.
The next level up is to use regular expressions, this will handle slightly more complex scenarios.
The best way is to use a full lexical analysis tool, which is probably overkill for the scenario you presented above. Would still be worth looking into just for fun.
This is a rather common problem so you should extend the string class with a flexible "Between" method. First you´ll have to define a extension class:
public static class StringExtensions
{
/// <summary>
/// Returnes a substring located between a leading substring (head) and following substring(tail).
/// Return null if head or tail are not part of this string.
/// </summary>
/// <param name="mainString"></param>
/// <param name="head">leading substring</param>
/// <param name="tail">following substring</param>
/// <returns>ubstring located between head and tail</returns>
public static String Between(this string mainString, string head, string tail)
{
int HeadPosition;
int TailPosition;
int ResultPosition;
int ResultLenght;
//test if mainstring contains head and tail
if (!mainString.Contains(head) && mainString.Contains(tail))
{
return null;
}
HeadPosition = mainString.IndexOf(head);
TailPosition = mainString.IndexOf(tail);
ResultPosition = HeadPosition + head.Length;
ResultLenght = TailPosition - ResultPosition;
return mainString.Substring(ResultPosition, ResultLenght);
}
}
Then all you have to do is call the new method on any string.
var logTime = "LastWrite Time Thu Dec 9 05:12:42 2010 (UTC)";
logTime = logTime.Between("Time","(UTC)").Trim();
Please note that "Between" will only be accessible if you use the namespace that you defined StringExtensions in.
精彩评论