【问题标题】:How to obtain this substring result in C#如何在 C# 中获取此子字符串结果
【发布时间】:2018-08-18 11:24:36
【问题描述】:

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

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

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

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

我想要 3323232 值。但我没有找到最好的解决方案。你可以帮帮我吗?我会尽可能以最有活力的方式去做。

3323232 后面的所有内容(例如 stuffs 和 examplePhoto)也可能不同。可以有其他名称或其他尺寸,如下所示:

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

谢谢

【问题讨论】:

  • 使用正则表达式 (RegEx)。 #https://www.example.com/examplePhoto/stuffs/(\d+)\?result=33#
  • 似乎它总是出现在字符串stuffs/ 之后和/? 之前,它应该很容易使用标准字符串函数。或者你可以使用正则表达式。你有没有尝试过?
  • 对不起,这些价值观背后的一切也随之改变。不总是东西examplePhoto(我编辑了我的帖子再次检查)
  • 但它总是以数字后面的第一个斜杠结束。应该很容易。
  • 由于您的示例字符串看起来很像 Urls,您可以考虑使用 System.Uri 类为您进行解析。这将处理诸如没有查询字符串的 Url 之类的情况。特别看属性Uri.Segmentsdocs.microsoft.com/en-us/dotnet/api/…

标签: c# asp.net string substring


【解决方案1】:

没有必要为此使用正则表达式。 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

【讨论】:

  • 我会采用这种方法;更容易,不需要字符串/正则表达式处理
  • 为我工作:D
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    以下代码是动态的,应该适用于您特定情况的所有场景。如果这对您不起作用,请告诉我。

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

    【讨论】:

      【解决方案4】:
      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】:

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

        【讨论】:

          【解决方案6】:

          在重新创建 stopper 之后,我可以理解替换要快得多。

          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
          

          【讨论】:

          • 是什么让它“最好”?
          【解决方案7】:

          这可能会对您有所帮助:

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

          【讨论】:

            猜你喜欢
            • 2023-03-28
            • 1970-01-01
            • 2011-10-04
            • 2015-03-25
            • 1970-01-01
            • 1970-01-01
            • 2012-10-24
            • 1970-01-01
            相关资源
            最近更新 更多