拆分文件名 [英] Splitting a filename

查看:233
本文介绍了拆分文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件名:

NewReport-20140423_17255375-BSIQ-2wd28830-841c-4d30-95fd-a57a7aege412.zip

如何分割上述文件名以4串组。

How can i split the above filename to 4 string groups.

NewReport
20140423_17255375
BSIQ
2wd28830-841c-4d30-95fd-a57a7aege412.zip

我想:

 string[] strFileTokens = filename.Split(new char[]{'-'} , StringSplitOptions.None);

但我没有收到上面要求四弦

but i'm not getting the above required four strings

推荐答案

您可以指定子字符串的最大数量返回:

You can specify the maximum number of substrings to return:

var strFileTokens = filename.Split(new[] { '-' }, 4, StringSplitOptions.None);

这将避免分裂 2wd28830-841c-4d30-95fd-a57a7aege412.zip 成额外的碎片。

This will avoid splitting 2wd28830-841c-4d30-95fd-a57a7aege412.zip into additional "pieces".

(此外,您还可以删除 StringSplitOptions.None 因为这是默认的。)

在内部,它创建了一个循环内子阵列,并且只循环的指定次数(或在您的字符串可用分隔符的最大数量,以较低者为准)。

Internally, it creates an array of substrings inside a loop, and it only loops the number of times you specify (or the maximum number of delimiters available in your string, whichever is less).

for (int i = 0; i < numActualReplaces && currIndex < Length; i++)
{
    splitStrings[arrIndex++] = Substring(currIndex, sepList[i] - currIndex );
    currIndex = sepList[i] + ((lengthList == null) ? 1 : lengthList[i]); 
}

变量 numActualReplaces 是最小的或者指定的数量,或实际分隔符present字符串中的数量。 (这样的话,如果你指定一个像30数量较多,但你只有7连字符的字符串,你不会得到一个例外。事实上,当你不指定一个数字都,它的实际使用 Int32.MaxValue 没有你甚至没有意识到它。)

The variable numActualReplaces is the minimum of either the count you specify, or the number of actual delimiters present in your string. (That way, if you specify a larger number like 30, but you only have 7 hyphens in the string, you won't get an exception. Actually, when you don't specify a number at all, it's actually using Int32.MaxValue without you even realizing it.)

您更新您的问题您使用的Silverlight,不具有他重载与计数说。这真是烦人,因为根据字符串类仍然支持它的引擎盖。他们只是没有提供一种方式来传递自己的价值。它采用了硬codeD int.MaxValue

You updated your question to say you're using Silverlight, which doesn't have he overloads with Count. That's really annoying, because under the hood the string class still supports it. They just didn't provide a way to pass in your own value. It uses a hard-coded int.MaxValue.

您可以创建自己的方法带回所期望的功能。我没有测试过这种扩展方法对所有边缘的情况下,但它与你的字符串工作。

You could create your own method to bring back the desired functionality. I haven't tested this extension method out for all edge-cases, but it does work with your string.

public static class StringSplitExtension
{
    public static string[] SplitByCount(this string input, char[] separator, int count)
    {
        if (count < 0)
            throw new ArgumentOutOfRangeException("count");

        if (count == 0)
            return new string[0];

        var numberOfSeparators = input.Count(separator.Contains);

        var numActualReplaces = Math.Min(count, numberOfSeparators + 1);

        var splitString = new string[numActualReplaces];

        var index = -1;
        for (var i = 1; i <= numActualReplaces; i++)
        {
            var nextIndex = input.IndexOfAny(separator, index + 1);
            if (nextIndex == -1 || i == numActualReplaces)
                splitString[i - 1] = input.Substring(index + 1);
            else
                splitString[i - 1] = input.Substring(index + 1, nextIndex - index - 1);
            index = nextIndex;
        }

        return splitString;
    }
}

请确保它的地方访问你的用户控件,然后调用它是这样的:

Make sure it's somewhere accessible to your UserControl, then call it like this:

var strFileTokens = filename.SplitByCount(new[] { '-' }, 4)

这篇关于拆分文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆