繁体   English   中英

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

[英]How to obtain this substring result in C#

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

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

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

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

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

同样,在3323232之后的所有内容(例如,东西和examplePhoto)也可以有所不同。 可以使用其他名称或其他尺寸,例如:

https://www.example.com/exampleVideos/stuffs_videos/3323232/?result=33

谢谢

无需为此使用正则表达式。 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

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

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);
}

重新创建塞子后,我可以了解到替换操作要快得多。

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

假设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);
        }
    }
}
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);

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

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();
        }
    }

这可以帮助您:

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM