-3
string s = "Text1 Text2 08/13/2014 08:17:22";

How can I take the string "text1 text2" and remove the DateTime?

I would like to get 1 string that has the value of "text1 text2"

Note, the text can be any text, the time can be any time but with the same DateTime Format

3
  • 1
    Any attempt? Text1 and Text2 are always single words space separated? Commented Aug 13, 2014 at 10:48
  • 1
    Is is always in that format? Is it always the last part of the string? Or do you always want the first two words? ...
    – Alex K.
    Commented Aug 13, 2014 at 10:48
  • I don't understand the close votes. It's pretty clear what is being asked. Commented Aug 13, 2014 at 12:22

5 Answers 5

2

Note, the text can be any text, the time can be any time but with the same DateTime Format

You could simply use String.Remove:

string result = s.Remove(s.Length - 19).Trim(); // skipped s.Length >= 19 check
2
  • Because the date-time string is 19 chars long
    – Alex K.
    Commented Aug 13, 2014 at 14:12
  • @Neel: yes, because OP mentioned that the text is variable but the format of the DateTime is strict. So the rule is: remove 19 characters from the end ("08/13/2014 08:17:22".Length). Commented Aug 13, 2014 at 15:09
0

You can always delete ending symbols.

s = s.Remove(s.Length-19);//19 is length of dates.
0
0

Why not use string.Substring?

string text = s.Substring(0, s.Length - "08/13/2014 08:17:22".Length);
via regex
string text = Regex.Match(s, @"([\s\S]*)\s?\d\d/\d\d/\d\d\d\d\s\d\d:\d\d:\d\d").Groups[1].Value;
2
  • 1
    Doesn't it need a '$' in there to indicate the end of the string match? Commented Aug 13, 2014 at 10:53
  • It's not necessary in that case.
    – Nisus
    Commented Aug 13, 2014 at 10:59
0

try below code I have tested in Visual studio and it works :-

 string s = string.Empty;
 string result = string.Empty;
 s = "Text1 Text2 08/13/2014 08:17:22";
 string[] split = s.Split(' ');
 foreach (string i in split.Take(2))
 {
      result += i;
 }
0

Here is C# code using regular expression:

string newString = Regex.Replace(s, @" \d{2}/\d{2}/\d{4} \d{2}\:\d{2}\:\d{2}", "");

Not the answer you're looking for? Browse other questions tagged or ask your own question.