2

How would you go about getting the substring of a string by passing the string the substring text that you are looking for. Also would you be able to combine regex with regular text without causing any issues

3
  • you can use .Contains but I am not really sure what you are asking Commented May 26, 2011 at 2:09
  • Could you give an example of how this should work? I'm just not sure what you are attempting to do. Commented May 26, 2011 at 2:11
  • Can you give more clarification on what you mean by the second part of your question? Commented May 26, 2011 at 2:39

2 Answers 2

4

If you want to know if a substring exists within a string you can use Contains. If you want to know the position of a substring within a string you can use IndexOf (which can also be used to see if it exists... see examples below).

Examples for checking existence of substring:

bool subStringExists = yourStringVariable.Contains("yourSubString");
bool subStringExists = yourStringVariable.IndexOf("yourSubString") >= 0;

Examples for finding position of substring:

int subStringPosition = yourStringVariable.IndexOf("yourSubString");

UPDATE:

Based on your comment about the URL matching, you can do it all with a regex expression. With regular expressions, you can have certain parts of the expression be literal, while other parts are variable. In the case of what you're trying to do you would have something like this for your regex:

// Will match on http://www.mywebsite.com/abc#.aspx, where # is 1 or more digits
const string regExCommand = "(http://www.mywebsite.com/abc\\d+\\.aspx)";

Here is a complete working example you can copy into a console project and play around with to find out exactly what you need:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace RegExExample
{
    public class Program
    {
        static void Main()
        {
            var urls = new List<Url>
            {
                new Url
                {
                    Name = "Match 1",
                    Address = "http://www.mywebsite.com/abc123.aspx"
                },
                new Url
                {
                    Name = "Match 2",
                    Address = "http://www.mywebsite.com/abc45.aspx"
                },
                new Url
                {
                    Name = "Match 3",
                    Address = "http://www.mywebsite.com/abc5678.aspx"
                },
                new Url
                {
                    Name = "No Match 1",
                    Address = "http://www.otherwebsite.com/abc123.aspx"
                    // No match because otherwebsite.com
                },
                new Url
                {
                    Name = "No Match 2",
                    Address = "http://www.mywebsite.com/def123.aspx"
                    // No match because def
                }
            };

            // Will match on http://www.mywebsite.com/abc#.aspx, where # is 1 or more digits
            const string regExCommand = "(http://www.mywebsite.com/abc\\d+\\.aspx)";

            var r = new Regex(regExCommand, RegexOptions.IgnoreCase | RegexOptions.Singleline);

            urls.ForEach(u =>
            {
                var m = r.Match(u.Address);
                if (m.Success)
                {
                    Console.WriteLine(String.Format("Name: {0}{1}Address: {2}{1}",
                                                    u.Name,
                                                    Environment.NewLine,
                                                    u.Address));
                }
            });

            Console.ReadLine();
        }
    }

    internal class Url
    {
        public string Name { get; set; }
        public string Address { get; set; }
    }
}

The output would be as follows:

Name: Match 1
Address: http://www.mywebsite.com/abc123.aspx

Name: Match 2
Address: http://www.mywebsite.com/abc45.aspx

Name: Match 3
Address: http://www.mywebsite.com/abc.5678.aspx

Sign up to request clarification or add additional context in comments.

4 Comments

Alright thanks the second one was my question. Can you also use a mixture of regular text and regex in that without causes any issues?
I'm still not clear on what you mean by that. Can you give an example of what you're trying to do?
Alright what I'm doing is parsing a webpage to try and find if anywhere on the page is listed a certain url. The url always has a different ending though, it always ends with a different set of numbers. Since I don't know these numbers, I was wondering if you could just substitute the numbers that I'm unsure of with regex(Regular Expressions) and keep the rest of the known url the same.
@Chris: I've updated my answer to with an example of what I think you're trying to do.
0

Since you are already know the substring, I assume you are checking to see if the string contains the substring; you can do

bool hasSubstring = str.Contains(subStr);

2 Comments

No I already knew how to check it, I was just unsure of how to get the position of it
str.IndexOf(subStr) will give you the position of first occurrence. there's also a LastIndexOf()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.