如何在C#中获取此子字符串结果

时间:2018-08-18 11:24:36

标签: c# asp.net string substring

我正面临一个问题,但没有找到解决问题的最佳方法。想象一下我有这个字符串:

  

https://blog.knoldus.com/using-gridfs-within-scala-to-store-large-files-in-mongodb/

获取子字符串 10156664251312164 值的最佳方法是什么?它可以有不同的大小,可能是这样:

  

https://www.example.com/examplePhoto/stuffs/10156664251312164/?result=33

我想拥有 3323232 值。但是我没有找到最好的解决方案。你可以帮帮我吗?我喜欢以最动态的方式来做。

此外,3232232之后的所有内容(例如,东西和examplePhoto)都可以不同。可以使用其他名称或其他尺寸,例如:

  

https://www.example.com/examplePhoto/stuffs/3323232/?result=33

谢谢

7 个答案:

答案 0 :(得分:4)

为此无需使用正则表达式。 Uri类是专门为解析URI而定义的。完整示例:

using System;
using System.Linq;

namespace UrlParserDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var uri = new Uri("https://www.example.com/examplePhoto/stuffs/10156664251312164/?result=33");
            var uri2 = new Uri("https://www.example.com/examplePhoto/stuffs/3323232/?result=33");
            var uri3 = new Uri("https://www.example.com/exampleVideos/stuffs_videos/3323232/?result=33");

            Console.WriteLine($"Example 1: {StripTrailingSlash(uri.Segments.Last())}");
            Console.WriteLine($"Example 2: {StripTrailingSlash(uri2.Segments.Last())}");
            Console.WriteLine($"Example 3: {StripTrailingSlash(uri3.Segments.Last())}");
        }

        private static string StripTrailingSlash(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                return "";
            }

            if (source.Last() != '/')
            {
                return source;
            }

            return source.Substring(0, source.Length - 1);
        }
    }
}

产生所需的输出:

Example 1: 10156664251312164 Example 2: 3323232 Example 3: 3323232

答案 1 :(得分:3)

如果您想获取数字部分,我认为这段代码应该可以工作:

string input = @"https://www.example.com/examplePhoto/stuffs/10156664251312164/?result=33";

var splitedList = input .Split('/');
foreach (var item in splitedList )
{
    int n;
    bool isNumeric = int.TryParse(item , out n);
    if(isNumeric)
         Console.WriteLine(item);
}

答案 2 :(得分:1)

重新创建塞子后,我知道替换的速度要快得多。

string input = @"https://www.example.com/examplePhoto/stuffs/10156664251312164/?result=33";

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

                string res1 = input.Split('/')[5];

            stopwatch.Stop();
            Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);

             stopwatch = new Stopwatch();

            stopwatch.Start();
            string match = Regex.Match(Regex.Match(input, @"[\/][\d]+[\/]").Value, @"[\d]+").Value;
            stopwatch.Stop();
            Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
             stopwatch = new Stopwatch();

            stopwatch.Start();
            string res = input.Replace(@"https://www.example.com/examplePhoto/stuffs/", "").Replace(@"/?result=33", "");
            stopwatch.Stop();
            Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);

Output
    Time elapsed: 00:00:00.0000097
Time elapsed: 00:00:00.0008232
Time elapsed: 00:00:00.0000064

答案 3 :(得分:1)

假设URL已经具有相同数量的分隔符“ /”,那么您可以在正确的索引上执行Split()并使用它。

例如,

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var examples = new List<string>
        {
            "https://www.example.com/exampleVideos/stuffs_videos/3323232/?result=33",
            "https://www.example.com/exampleVideos/stuffs_videos/3323232/?result=33"
        };

        int idPositionInUrl = 5;

        foreach (var url in examples)
        {
            var id = url.Split('/')[idPositionInUrl];

            Console.WriteLine("Id: " + id);
        }
    }
}

答案 4 :(得分:1)

string myInput = @"https://www.example.com/examplePhoto/stuffs/10156664251312164/?result=33";
myInput = myInput.Replace(@"https://www.example.com/examplePhoto/stuffs/", "");
var RESULT = myInput.Split('/').ElementAt(0);

答案 5 :(得分:1)

下面的代码是动态的,并且可以在特定情况下的所有情况下工作。让我知道这是否不适合您。

class Program
    {
        static void Main(string[] args)
        {
            string s = "https://www.example.com/examplePhoto/stuffs/3323232/?result=33";
            var endingstring = "/?result=";
            var strings = s.Substring(0, s.IndexOf(endingstring));
            var len = strings.LastIndexOf("/")+1;
            var thestringineed = strings.Substring(len);            
            Console.WriteLine("The string that i need is " + thestringineed);
            Console.ReadKey();
        }
    }

答案 6 :(得分:1)

这可以帮助您:

string myInput = @"https://www.example.com/examplePhoto/stuffs/10156664251312164/?result=33";
myInput = myInput.Replace(@"https://www.example.com/examplePhoto/stuffs/", "");
var RESULT = myInput.Split('/').ElementAt(0);