Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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_Split - Fatal编程技术网

C# 从两个字符之间的字符串获取文本

C# 从两个字符之间的字符串获取文本,c#,string,split,C#,String,Split,我正试图拉开一条线,把文本放在两个“#”之间。我已经找到了在Java和php中实现这一点的方法,我假设它们在C#中是相似的,我只是吉普失败了,并且非常确定它的PEBKAC。所以我想我会问 示例-我想通过编程从该字符串中提取“filenameid”和“Name”: “#filenameid#30day#Name#.xls”使用捕获组 @"#([^#]*)#" 从组索引1中获取所需的字符串。请注意,lookarounds在此处不起作用。尝试拆分: 然后取适当的块: String id = ch

我正试图拉开一条线,把文本放在两个“#”之间。我已经找到了在Java和php中实现这一点的方法,我假设它们在C#中是相似的,我只是吉普失败了,并且非常确定它的PEBKAC。所以我想我会问

示例-我想通过编程从该字符串中提取“filenameid”和“Name”:

“#filenameid#30day#Name#.xls”

使用捕获组

@"#([^#]*)#"
从组索引1中获取所需的字符串。请注意,lookarounds在此处不起作用。

尝试拆分:

然后取适当的块:

  String id = chunks[0];
  String period = chunks[1];
  String name = chunks[2];

如果字符串的格式始终相同,则可以执行以下操作:

string a = "#filenameid#30day#Name#.xls";
string[]split=a.Split('#');
string fileID = split[1];
string name = split[3];

您可以使用Regex实现它

void Main()
{
    const string Expression = @"#([^#]*)*#";
    const string TestSample = @"'#filenameid#30day#Name#.xls'";

    Regex regex = new Regex(Expression);

    regex.Matches(TestSample)
         .Cast<Match>()
         .Select(match => match.Captures[0].Value.Replace("#", ""))
         .ToList()
         .ForEach(Console.WriteLine);
}
void Main()
{
常量字符串表达式=@“#”([^#]*)*#”;
常量字符串TestSample=@“#filenameid#30day#Name#.xls”;
正则表达式正则表达式=新正则表达式(表达式);
regex.Matches(TestSample)
.Cast()
.Select(match=>match.Captures[0]。Value.Replace(“#“,”))
托利斯先生()
.ForEach(控制台写入线);
}

以下是该问题的低级解决方案:

static void Main(string[] args) {
    string text = "#filenameid#30day#Name#.xls";

    int frameStart = 0;
    int match = 0;
    // loop on characters
    for(int i = 0; i < text.Length; i++) {
        char c = text[i];
        switch(c) {
            case '#':
                // evaluate frame (text between meshes)
                switch(match) {
                    // match at index 1
                    case 1:
                        Console.Write("filenameid=");
                        Console.WriteLine(text.Substring(frameStart, i - frameStart));
                        break;
                    // match at index 3
                    case 3:
                        Console.Write("name=");
                        Console.WriteLine(text.Substring(frameStart, i - frameStart));
                        break;
                }
                // move to next frame
                frameStart = i + 1;
                match++;
                break;
        }
    }
    // count of matches is match + 1
    Console.ReadKey();
}
static void Main(字符串[]args){
字符串text=“#filenameid#30day#Name#.xls”;
int frameStart=0;
int匹配=0;
//循环字符
for(int i=0;i
您可以显示您尝试过的内容。您可以尝试以下操作:)您需要指定
StringSplitOptions。将mptyentries
删除到
Split
函数,文件id将位于索引0处。否则它在1,name在3。@juharr或adjustindices@BinkanSalaryman我很确定我在第二句话中提到了这一点。它会起作用的。问题是字符串的格式不会每次都相同,而且“#”之间的单词可能不同。我试图弄清楚如何得到哪些块在“#”之间,如果这有意义的话(如上所述),在#之间有多少个字母无关紧要,因为它将字符串按#分割成数组。字符串中的#的数量将为您提供数组中不同数量的项。因此,如果您的filenameID和Name以类似的模式后跟#,那么您就不会有任何问题,因为他需要两个字符串。组索引1将只包含“filenameid”。@Petrichor只使用进行全局匹配的方法。该方法返回“#filenameid#”。这会起作用。问题是字符串的格式不会每次都相同,“#”之间的单词可能不同。我正在试图弄清楚,如果这有意义的话,如何获取在“#”之间的块。为什么需要手动拆分字符串?对于这样一个简单的任务来说,噪音太大了。它具有最好的性能和灵活性,并演示了文本搜索算法ASM实现将更加灵活,并将提供更好的性能:)对不起,讽刺,我理解你的意思
static void Main(string[] args) {
    string text = "#filenameid#30day#Name#.xls";

    int frameStart = 0;
    int match = 0;
    // loop on characters
    for(int i = 0; i < text.Length; i++) {
        char c = text[i];
        switch(c) {
            case '#':
                // evaluate frame (text between meshes)
                switch(match) {
                    // match at index 1
                    case 1:
                        Console.Write("filenameid=");
                        Console.WriteLine(text.Substring(frameStart, i - frameStart));
                        break;
                    // match at index 3
                    case 3:
                        Console.Write("name=");
                        Console.WriteLine(text.Substring(frameStart, i - frameStart));
                        break;
                }
                // move to next frame
                frameStart = i + 1;
                match++;
                break;
        }
    }
    // count of matches is match + 1
    Console.ReadKey();
}