C# removing substring from end of string
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 b开发者_如何学Goest 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.
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
}
foreach (string suffix in remove)
{
if (yourString.EndsWith(suffix))
{
yourString = yourString.Remove(yourString.Length - suffix.Length);
break;
}
}
I think that BrokenGlass's solution is a good one, but personally I would prefer to create three separate methods allowing the user to trim only the start, end or both.
If these bothers were going to be used a lot, I would create them in a helper class and/or as extension methods; http://msdn.microsoft.com/en-gb/library/vstudio/bb383977.aspx
public string TrimStart(string inputText, string value, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase)
{
if (!string.IsNullOrEmpty(value))
{
while (!string.IsNullOrEmpty(inputText) && inputText.StartsWith(value, comparisonType))
{
inputText = inputText.Substring(value.Length - 1);
}
}
return inputText;
}
public string TrimEnd(string inputText, string value, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase)
{
if (!string.IsNullOrEmpty(value))
{
while (!string.IsNullOrEmpty(inputText) && inputText.EndsWith(value, comparisonType))
{
inputText = inputText.Substring(0, (inputText.Length - value.Length));
}
}
return inputText;
}
public string Trim(string inputText, string value, StringComparison comparisonType = StringComparison.CurrentCultureIgnoreCase)
{
return TrimStart(TrimEnd(inputText, value, comparisonType), value, comparisonType);
}
With these methods we can modify the code for looping through the array containing the strings to be trimmed.
var content = "08:00 AM";
var removeList = new [] { "a", "am", "p", "pm" };
for (var i = 0; i < removeList.length; i++)
{
content = TrimEnd(content, removeList[i]);
}
NOTE: This code could be optimized further, but will work as it is with a good speed.
Based on your edit, consider using the built-in DateTime.TryParse or DateTime.Parse methods followed by a String.Format. Here's a link to a good resource on formatting strings as well.
Sounds like a good place for regex.
using System.Text.RegularExpressions;
public bool IsValidTime(string thetime)
{
Regex checktime = new Regex(@"^(20|21|22|23|[01]\d|\d)(([:][0-5]\d){1,2})$");
return checktime.IsMatch(thetime);
}
Assuming you want time in the format xx:xx
var output = (from x in remove
where input.EndsWith(x)
select input.Substring(0, input.Length - x.Length)).FirstOrDefault() ?? input;
You can probably use DateTime
as your primary type then.
Use DateTime.TryParse
to read the time the user enters. And use DateTime.ToString
to reformat the time using whatever format you need.
char[] remove = { 'a', 'p', 'm' };
string s = "8:14pm";
s = s.TrimEnd(remove);
精彩评论