I have a string that is being submitted by our customers from an application on which I cannot put a validation on their end to restrict sending those kind of strings. So, I am trying to write a C# function to split the string into a key value pair / make sure the string matches a format so that the split into key value will be easy.
Here are my examples (ABCD is common in here) and the answer I get should be in the form of below for the examples
ABCD 1234 should be --> ABCD-1234
ABCD 1234 (two or theree spaces in between) should be ABCD-1234
ABCD 1234 should be(starts with a space) --> ABCD-1234
ABCD XX1234 should be --> ABCD-XX1234
ABCD--Z1234 should be --> ABCD-Z1234
ABCDTE ST QA1234 should be --> ABCD-QA1234
A-BCD 1234 ABCD-1234
Also, if possible, I will need this as well integrated into the same function for the above.
ABCDE-GHI491803 Should be WXYZ-491803
ABCDEFGH CT542021 Should be WXYZ-CT542021
for some of your requirements you can write extension method for string, for example:
public static class StringExtensions
{
public static string FormatMyString(this string input)
{
input = Regex.Replace(input, @"-+", " ");
return Regex.Replace(input, @"\s+", " ").Replace(" ", "-");
}
}
then in your code you can use it as follows:
var formatted = "ABCD-- 1234".FormatMyString();
but replacing letters...I don't see any logic there...how many beers did you have?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.