开发者

C# Method to return the part of string

I am using c# .net 2.0.

I have got below string with me

string str = "tcm:0-433-1";

Now I want to create a method in C#, which will take above string as input p开发者_运维百科arameter and will return "tcm:433-".

Please suggest the best way for it!


result = Regex.Replace(input, @"^(.+:)\d-(\d+-).*$", "$1$2");

Explaining the regular expression:

  • ^ -> Start of string
  • .+ -> anything one or more times
  • (.+:) -> first grouping
  • \d- -> a digit followed by dash
  • \d+ -> a digit one or more times
  • $ -> end of string
  • $1$2 -> first and second grouping


Update: Here's a bit of a nutty idea. One possible way of describing your desired input/output (to me) is this: you want to take everything up to and including the : character, then skip up through the first - character, then take again up through the following - character.

Generalizing this idea, I came up with the following mess:

// Awful name, obviously.
public static string TakeSkipTakeEtc(this string source, params char[] tokens)
{
    bool taking = true;

    int startIndex = 0;
    StringBuilder stringBuilder = new StringBuilder();
    foreach (char token in tokens)
    {
        int index = source.IndexOf(token, startIndex);
        if (index == -1)
        {
            if (taking)
            {
                stringBuilder.Append(source.Substring(startIndex));
            }

            return stringBuilder.ToString();
        }

        if (taking)
        {
            int length = index + 1 - startIndex;
            stringBuilder.Append(source.Substring(startIndex, length));
        }

        startIndex = index + 1;
        taking = !taking;
    }

    return stringBuilder.ToString();
}

This extension method on string takes a variable number of char arguments, which it uses to employ basically the same logic (take through A, then skip through B, then take through C, etc.) for any arguments.

So, for example:

Console.WriteLine("tcm:0-433-1".TakeSkipTakeEtc(':', '-', '-'));

The above outputs:

tcm:433-

Is this probably an overly complex solution to your problem? Most likely. But hey, it was a fun little challenge (I don't get enough of those these days).


So, given your particular input and desired output, you could do something like:

return str.Substring(0, 4) + str.Substring(6, 4);

Obviously, this is a very shaky solution that requires its input to match a specific format (at the very least, it needs to be 10 characters long). If your input is going to be too dynamic for such a rigid approach, you might consider using string.IndexOf to search for key split characters or possibly even the Regex class (yikes!) to achieve your desired result.

In this particular case you could use string.IndexOf to find the location of the first - character and then string.Substring to take only the portion of the string up to that index.


Maybe you need to show us more input/output examples, so that we can understand better the rule. Based on your one example, this would do it:

string str = "tcm:0-433-1";

int token1 = str.IndexOf(":");
int token2 = str.IndexOf("-");
int token3 = str.LastIndexOf("-");

if (token1 == -1 || token2 == -1 || token3 == -1)
    throw new YourException("invalid input");

string result = str.Substring(0, token1) + 
                str.Substring(token2 + 1, token3 - token2);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜