Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过在字符串中搜索子字符串文本来获取字符串的子字符串?_C#_String - Fatal编程技术网

C# 通过在字符串中搜索子字符串文本来获取字符串的子字符串?

C# 通过在字符串中搜索子字符串文本来获取字符串的子字符串?,c#,string,C#,String,如何通过将要查找的子字符串文本传递给字符串来获取字符串的子字符串。另外,您是否能够将正则表达式与常规文本结合起来而不引起任何问题因为您已经知道子字符串,我假设您正在检查字符串是否包含子字符串;你能行 bool hasSubstring = str.Contains(subStr); 如果想知道字符串中是否存在子字符串,可以使用。如果您想知道子字符串在字符串中的位置,您可以使用它(也可以使用它来查看它是否存在…请参见下面的示例) 检查子字符串是否存在的示例: bool subStringExis

如何通过将要查找的子字符串文本传递给字符串来获取字符串的子字符串。另外,您是否能够将正则表达式与常规文本结合起来而不引起任何问题

因为您已经知道子字符串,我假设您正在检查字符串是否包含子字符串;你能行

bool hasSubstring = str.Contains(subStr);

如果想知道字符串中是否存在子字符串,可以使用。如果您想知道子字符串在字符串中的位置,您可以使用它(也可以使用它来查看它是否存在…请参见下面的示例)

检查子字符串是否存在的示例:

bool subStringExists = yourStringVariable.Contains("yourSubString");
bool subStringExists = yourStringVariable.IndexOf("yourSubString") >= 0;
int subStringPosition = yourStringVariable.IndexOf("yourSubString");
查找子字符串位置的示例:

bool subStringExists = yourStringVariable.Contains("yourSubString");
bool subStringExists = yourStringVariable.IndexOf("yourSubString") >= 0;
int subStringPosition = yourStringVariable.IndexOf("yourSubString");
更新:

根据您对URL匹配的评论,您可以使用正则表达式完成所有操作。对于正则表达式,可以让表达式的某些部分是文字,而其他部分是可变的。在您尝试执行的情况下,您的正则表达式将具有以下内容:

// 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)";
下面是一个完整的工作示例,您可以将其复制到控制台项目中,并使用它来了解您到底需要什么:

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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
名称空间regexample
{
公共课程
{
静态void Main()
{
var url=新列表
{
新网址
{
Name=“匹配1”,
地址=”http://www.mywebsite.com/abc123.aspx"
},
新网址
{
Name=“匹配2”,
地址=”http://www.mywebsite.com/abc45.aspx"
},
新网址
{
Name=“匹配3”,
地址=”http://www.mywebsite.com/abc5678.aspx"
},
新网址
{
Name=“不匹配1”,
地址=”http://www.otherwebsite.com/abc123.aspx"
//不匹配,因为otherwebsite.com
},
新网址
{
Name=“不匹配2”,
地址=”http://www.mywebsite.com/def123.aspx"
//没有匹配,因为def
}
};
//将在http://www.mywebsite.com/abc#.aspx,其中#是一个或多个数字
常量字符串regExCommand=”(http://www.mywebsite.com/abc\\d+\\\.aspx)”;
var r=新正则表达式(regExCommand,RegexOptions.IgnoreCase | RegexOptions.Singleline);
URL.ForEach(u=>
{
var m=r.匹配(u.地址);
如果(m.成功)
{
Console.WriteLine(String.Format(“名称:{0}{1}地址:{2}{1}”),
u、 名字,
环境,新线,
u、 地址);;
}
});
Console.ReadLine();
}
}
内部类Url
{
公共字符串名称{get;set;}
公共字符串地址{get;set;}
}
}
产出如下:

名称:匹配1
地址:

名称:匹配2
地址:

名称:匹配3

地址:

您可以使用。Contains,但我不确定您在问什么。您能举个例子说明这应该如何工作吗?我只是不知道你想做什么。你能对你问题的第二部分做进一步的澄清吗?好的,谢谢,第二部分是我的问题。你能同时使用普通文本和正则表达式吗?我还不清楚你的意思。你能举一个例子说明你在做什么吗?好的,我正在做的是解析一个网页,试图找出网页上的任何地方是否列出了某个url。虽然url总是有不同的结尾,但它总是以不同的数字集结尾。因为我不知道这些数字,我想知道你是否可以用正则表达式(regex)替换我不确定的数字,并保持已知url的其余部分不变。@克里斯:我已经用一个例子更新了我的答案,说明了我认为你在做什么。不,我已经知道如何检查它了,我只是不确定如何获得它的位置
str.IndexOf(subStr)
将为您提供第一次出现的位置。还有一个
LastIndexOf()