Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# removing substring from end of string

Tags:

string

c#

textbox

I have an array of strings:

string[] remove = { "a", "am", "p", "pm" }; 

And I have a textbox that a user enters text into. If they type any string in the remove array at the end of the text in the textbox, it should be removed. What is the easiest way to do this?

EDIT To clarify, I'm making a time parser. When you give the function a string, it does its best to parse it into this format: 08:14pm I have a textbox to test it. When the focus leaves the textbox, I need to get the text without the am/pm/a/p suffix so I can parse the number only segment.

like image 876
Entity Avatar asked Nov 04 '10 21:11

Entity


1 Answers

string[] remove = { "a", "am", "p", "pm" }; string inputText = "blalahpm";  foreach (string item in remove)     if (inputText.EndsWith(item))     {         inputText = inputText.Substring(0, inputText.LastIndexOf(item));         break; //only allow one match at most     } 
like image 108
BrokenGlass Avatar answered Nov 08 '22 22:11

BrokenGlass