0

For example, if I have 'MD 1001 OD', I'd want it to remove the 'MD '

but if its just '1001 OD', I want it to do nothing.

My purpose is for trimming leading letters (not numbers) from string that have them only up to the first space.

For this I dont believe there will ever be more than 2 leading letters, but it technically could be possible.

Derek Meyer
  • 477
  • 5
  • 20
  • I have a big list of strings, some of which have leading letters, some of which dont. I am wanting to only removed any leading letters up to the first space if they exist. – Derek Meyer Feb 26 '20 at 14:55
  • `"MD 1001 OD".TakeWhile(x => char.IsLetter(x))` will give you all the leading chars up until digits or whitespace. – maccettura Feb 26 '20 at 14:55
  • 1
    Well, your expectation is not quite clear. You say, you want to get `MD` if the string is `MD 1001 OD`, but now you say you want to remove the leading letters. Thus, please show the code that fails to clarify what exactly you are doing. – Wiktor Stribiżew Feb 26 '20 at 14:57
  • I am clarifying now. I want to removed leading letters up to the first space if they exist. I edited my question description. – Derek Meyer Feb 26 '20 at 14:58
  • You do not need Regex : string input = "MD 1001 OD"; string output = input.Substring(0, input.IndexOf(" ")); if(input.StartsWith("MD")){} – jdweng Feb 26 '20 at 15:01
  • `input.SkipWhile(x => char.IsLetter(x) || char.IsWhiteSpace(x)` – maccettura Feb 26 '20 at 15:02
  • @jdweng the first few letters could be any number of letter combinations, not necessarily 'MD'. And the string may not have leading letters. – Derek Meyer Feb 26 '20 at 15:02
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/a/2759417/3832970) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Feb 26 '20 at 15:04
  • I'm just extracting up to the first space. You can then check the anything you want in the IF. – jdweng Feb 26 '20 at 15:07
  • @jdweng thanks this is want I needed. – Derek Meyer Feb 26 '20 at 15:25

0 Answers0