Вопрос

I'm trying to parse this string 'Smith, Joe M_16282' to get everything before the comma, combined with everything after the underscore.

The resulting string would be: Smith16282

Это было полезно?

Решение

string longName = "Smith, Joe M_16282";

string shortName = longName.Substring(0, longName.IndexOf(",")) + longName.Substring(longName.LastIndexOf("_") + 1);

Notes:

  • The second "substring" doesn't need a length parameter, because we want everything after the underscore
  • The LastIndexOf is used instead of IndexOf in case there are other underscores appearing in the name such as "Smith_Jones, Joe M_16282"
  • This code assumes that there is at least one comma and at least one underscore in the string "longName." If not, the code fails. I will leave that checking to you if you need it.

Другие советы

As others have said, the simple approach for parsing a string like that would be to use the String's various parsing methods, such as IndexOf and SubString. If you want something more powerful and flexible, you may also want to consider using a RegEx replacement. For instance, you could do something like this:

Dim input As String = "Smith, Joe M_16282"
Dim pattern As String = "(.*?),.*?_(.*)"
Dim replacement As String = "$1$2"
Dim output As String = Regex.Replace(input, pattern, replacement)

Or, more simply:

Dim output As String = Regex.Replace("Smith, Joe M_16282", "(.*?),.*?_(.*)", "$1$2")

Here's the meaning of the pattern:

  • (.*?) - The first group capturing all of the characters before the comma
    • ( - Starts the capturing group
    • . - This is a wildcard which matches any character
    • * - Specifies that the previous thing (any character) is repeated any number of times
    • ? - Specifies that the * is non-greedy, meaning it won't match everything until the end of the string--it will only match until it finds the following comma
    • ) - Ends the capturing group
  • , - The comma to look for
  • .*? - Says that there will be any number of any characters between the comma and the underscore which we don't care about
    • . - Any character
    • * - Any number of times
    • ? - Until you find the underscore
  • _ - The underscore the look for
  • (.*) - The second group capturing all of the characters after the underscore
    • ( - Starts the capturing group
    • . - Any character
    • * - Any number of times
    • ) - Ends the capturing group

Here's the meaning of the replacement:

  • $1 - The value of all of the characters found in the first capturing group
  • $2 - The value of all of the characters found in the second capturing group

RegEx may be overkill for your particular situation, but it is a very handy tool to learn. One major advantage is that you could move the pattern and replacement values out into external settings in the app.config, or somewhere. Then, you could modify the replacement rules without recompiling your application.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top