在 C# 中将字符串切成两个或多个部分

发布于 2024-11-03 10:19:59 字数 268 浏览 1 评论 0原文

我有一个像这样的字符串(c#,winforms):

private string source = @"CDM_DEBUG\D1_XS1000psc_1"

我想将此字符串分为两部分,第一部分应该是最后一个下划线之前的所有内容,即“CDM_DEBUG\D1_XS1000psc”,第二部分应该是“_1”。

然后我想从第一部分创建一个新字符串并将其设置为“CDM_DEBUG\D1_XS1000psc_2”

最快的方法是什么?

I have a string like this (c#, winforms):

private string source = @"CDM_DEBUG\D1_XS1000psc_1"

I want to have this string in two parts, first part should be everything before the last underscore that will be 'CDM_DEBUG\D1_XS1000psc' and second part should be '_1'.

Then I want to make a new string from the first part and make it to 'CDM_DEBUG\D1_XS1000psc_2'

what is the fastest way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

当爱已成负担 2024-11-10 10:19:59

查看String.LastIndexOf

int lastUnderscore = source.LastIndexOf('_');
if (lastUnderscore == -1)
{
    // error, no underscore
}

string firstPart = source.Substring(0, lastUnderscore);
string secondPart = source.Substring(lastUnderscore);

它比正则表达式更快吗?可能吧。可能不是。

Check out String.LastIndexOf.

int lastUnderscore = source.LastIndexOf('_');
if (lastUnderscore == -1)
{
    // error, no underscore
}

string firstPart = source.Substring(0, lastUnderscore);
string secondPart = source.Substring(lastUnderscore);

Is it faster than regular expressions? Possibly. Possibly not.

逆夏时光 2024-11-10 10:19:59

也许这样的东西会起作用?

private const char fileNumberSeparator = '_';

private static string IncrementFileName(string fileName)
{
    if (fileName == null)
        throw new ArgumentNullException("fileName");
    fileName = fileName.Trim();
    if (fileName.Length == 0)
        throw new ArgumentException("No file name was supplied.", "fileName");
    int separatorPosition = fileName.LastIndexOf(fileNumberSeparator);
    if (separatorPosition == -1)
        return AppendFileNumber(fileName, 1);
    string prefix = fileName.Substring(0, separatorPosition);
    int lastValue;
    if (int.TryParse(fileName.Substring(separatorPosition + 1, out lastValue)
        return AppendFileNumber(prefix, lastValue + 1);
    else
        return AppendFileNumber(fileName, 1);
}

private static string AppendFileNumber(string fileNamePrefix, int fileNumber)
{
    return fileNamePrefix + fileNumberSeparator + fileNumber;
}

Maybe something like this would work?

private const char fileNumberSeparator = '_';

private static string IncrementFileName(string fileName)
{
    if (fileName == null)
        throw new ArgumentNullException("fileName");
    fileName = fileName.Trim();
    if (fileName.Length == 0)
        throw new ArgumentException("No file name was supplied.", "fileName");
    int separatorPosition = fileName.LastIndexOf(fileNumberSeparator);
    if (separatorPosition == -1)
        return AppendFileNumber(fileName, 1);
    string prefix = fileName.Substring(0, separatorPosition);
    int lastValue;
    if (int.TryParse(fileName.Substring(separatorPosition + 1, out lastValue)
        return AppendFileNumber(prefix, lastValue + 1);
    else
        return AppendFileNumber(fileName, 1);
}

private static string AppendFileNumber(string fileNamePrefix, int fileNumber)
{
    return fileNamePrefix + fileNumberSeparator + fileNumber;
}
各自安好 2024-11-10 10:19:59
string doc = @"CDM_DEBUG\D1_XS1000psc_1";
var lastPos = doc.LastIndexOf("_");
if(lastPos!=-1){
   string firstPart = doc.Substring(0,lastPos);
   string secondPart = doc.Substring(lastPos);
   var nextnumber = Int32.Parse(secondPart.TrimStart('_'))+1;
   var output = firstPart + "_" + nextnumber;
}
string doc = @"CDM_DEBUG\D1_XS1000psc_1";
var lastPos = doc.LastIndexOf("_");
if(lastPos!=-1){
   string firstPart = doc.Substring(0,lastPos);
   string secondPart = doc.Substring(lastPos);
   var nextnumber = Int32.Parse(secondPart.TrimStart('_'))+1;
   var output = firstPart + "_" + nextnumber;
}
记忆で 2024-11-10 10:19:59

指数和范围 C# 8.0+ 中提供了功能,允许您使用以下简洁语法拆分字符串:

var firstPart = source[..^2]; 

var secondPart = source[^2..];

Indices and ranges functionality is available in C# 8.0+ allowing you to split the string with the following succinct syntax:

var firstPart = source[..^2]; 

var secondPart = source[^2..];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文