André Azevedo
André Azevedo

Reputation: 237

How to obtain this substring result in C#

I am facing a problem and I am not finding the best solution to solve it. Imagine I have this string:

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

What is the best way to obtain the substring 10156664251312164 value? It can have different sizes it could be this:

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

And I would like to have the 3323232 value. But I am not finding the bests solution to this. Could you help me? I would liek to do in the most dynamic way possible.

Also everything that is behind 3323232 like stuffs and examplePhoto can be different. Can have another names or another sizes like this:

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

Thank you

Upvotes: 0

Views: 155

Answers (7)

John H
John H

Reputation: 21

This may help you:

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

Upvotes: 1

ravichandra vydhya
ravichandra vydhya

Reputation: 969

After recreating stopper I could understand that replace is much faster.

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

Upvotes: 1

John H
John H

Reputation: 14655

There is no need to use regular expressions for this. The Uri class was defined specifically to parse URIs. Complete example:

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

Produces the desired output of:

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

Upvotes: 4

ScottBurfieldMills
ScottBurfieldMills

Reputation: 196

Assuming the URL already has the same number of separators '/' then you can perform a Split() on the correct index and use that.

For example,

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

Upvotes: 1

Hossein
Hossein

Reputation: 3113

I think this code should work , if you want get Numeric section :

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

Upvotes: 3

Habil Harati
Habil Harati

Reputation: 192

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

Upvotes: 1

Praveen Raju
Praveen Raju

Reputation: 64

Below code is dynamic and should work in all scenarios for your particular situation. Let me know if this does not work for you.

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

Upvotes: 1

Related Questions