2

所以我有示例网址http://citrine.rcd.dev:8080/image/1582_125x125.jpg

它是用以下代码构建的:

    public string GetDerivativeUrl(long imageId, bool preserveTransparency, DerivativeSize size, bool allowCache, string designTags, int quality)
    {
        string tags = (designTags == null || designTags.Trim().Length == 0) ? "" : designTags;
        string imageFormat = (preserveTransparency) ? "png" : "jpg";
        if (quality > 0)
            return String.Format("{0}/image/{4}{1}_{2}.{3}?qv={5}", GetUrlPrefix(imageId, allowCache), imageId, size.GetPixelSize(), imageFormat, tags, quality);
        return String.Format("{0}/image/{4}{1}_{2}.{3}", GetUrlPrefix(imageId, allowCache), imageId, size.GetPixelSize(), imageFormat, tags);
    }

我想http://citrine.rcd.dev:8080/image/1582.jpghttp://citrine.rcd.dev:8080/image/1582_125x125.jpg

我该怎么做呢?

4

2 回答 2

1

使用System.Uri从路径中获取文件名部分,然后替换"_.*?\.""."

于 2013-07-29T11:49:24.870 回答
1

您可以这样做而不是使用正则表达式。

string url = @"http://citrine.rcd.dev:8080/image/1582_125x125.jpg";
int index = url.LastIndexOf('_');
if (index != -1)
   url = url.Substring(0, index) + Path.GetExtension(url);
于 2013-07-29T11:49:32.787 回答