简体   繁体   English

String。包含int吗?

[英]String.Contains int?

I am wondering how to write this part of the code .. 我想知道如何编写这部分代码..

I would like to say that if the name of my folder contains R + whatever the int ( R1, R52 etc ), then I do an action. 我想说的是,如果我的文件夹名称包含R +以及所有int值(R1,R52等),那么我将执行一个操作。

This is what I try to use : 这是我尝试使用的方法:

 if(f.Name.Contains("R" + int))
 {

 }

This is a good time to use Regex 这是使用Regex的好时机

if(Regex.IsMatch(f.Name, @"R\d")) {
  ...
}

Use the string representation of the int? 使用int的字符串表示形式?

if(f.Name.Contains("R" + i.ToString()))
{

}

Or do you want to match any integer? 还是要匹配任何整数? You'll have to run a regex for that. 您必须为此运行一个正则表达式。 Probably just "R\\d+" as the expression. 可能只是“ R \\ d +”作为表达式。

Try using Regex: 尝试使用正则表达式:

if(Regex.IsMatch(f.Name, "R[0-9]"))
{
   // This will only execute if the String starts with R
   // and is followed by 1 or more numbers
}
if (f.Name.Contains ("R" + Convert.ToString(i)))
{
    // Your code here
}

What about this? 那这个呢?

if(r.Name.StartsWith("R"))
{

}

Or you can use switch : 或者您可以使用switch:

string caseSwitch = f.Name;
switch (caseSwitch)
{
    case "R1": 
        Console.WriteLine("Case R1");
        break;
    case "R2":
        Console.WriteLine("Case R2");
        break;
    default:
        Console.WriteLine("Default case");
        break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM