如何使用 C# 中的字符分隔符将字符串分成两部分?

发布于 2024-11-09 04:10:25 字数 172 浏览 2 评论 0原文

使用单字符分隔符将字符串分成两部分的最佳方法是什么?

应在分隔符的第一个实例上拆分字符串。该方法应考虑性能。它不应该假设字符串中存在分隔符、字符串有任何字符等;应该是通用代码,您可以将其插入到任何需要的地方。

(每当我需要的时候,我总是需要花几分钟来重写这类事情,所以我想我应该为此提出一个问题)

What's the best way to split a string into just two parts using a single-char separator?

The string should be split on the first instance of the separator. The method should consider performance. It shouldn't assume that the separator exists in the string, that the string has any characters, etc; should be general-purpose code you can just plug in wherever you need.

(It always takes me a few minutes to rewrite this sort of thing whenever I need it, so I thought I'd make a question for it)

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

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

发布评论

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

评论(4

锦上情书 2024-11-16 04:10:25

如果您确实只想得到两个结果,请使用带有第二个参数的字符串拆分方法:

string[] words = myString.Split(new char[]{' '}, 2);

If you really want to have just two results, use the string split method with a 2nd parameter:

string[] words = myString.Split(new char[]{' '}, 2);
森林迷了鹿 2024-11-16 04:10:25
var part1 = myString.SubString(0, myString.IndexOf(''));
var part2 = myString.SubString(myString.IndexOf(''), myString.Lenght);
var part1 = myString.SubString(0, myString.IndexOf(''));
var part2 = myString.SubString(myString.IndexOf(''), myString.Lenght);
终难遇 2024-11-16 04:10:25
    string[] SplitStringInTwo(string input, char separator)
    {
        string[] results = new string[2];
        if (string.IsNullOrEmpty(input)) return results;
        int splitPos = input.IndexOf(separator);
        if (splitPos <= 0) return results;
        results[0] = input.Substring(0, splitPos);
        if (splitPos<input.Length)
            results[1] = input.Substring(splitPos + 1);
        return results;
    }
    string[] SplitStringInTwo(string input, char separator)
    {
        string[] results = new string[2];
        if (string.IsNullOrEmpty(input)) return results;
        int splitPos = input.IndexOf(separator);
        if (splitPos <= 0) return results;
        results[0] = input.Substring(0, splitPos);
        if (splitPos<input.Length)
            results[1] = input.Substring(splitPos + 1);
        return results;
    }
温柔少女心 2024-11-16 04:10:25

(每当我需要的时候,我总是需要花几分钟来重写这类东西,所以我想我应该为此提出一个问题)

如果你经常需要这个,你可以将你喜欢的方式转换为 扩展方法。根据 Teoman Soygul 的建议:

public static class StringExtensions
{
  public static string[] TwoParts(this String str, char splitCharacter)
  {
    int splitIndex = str.IndexOf(splitCharacter);
    if(splitIndex == -1)
      throw new ArgumentException("Split character not found.");

    return new string[] {
      str.SubString(0, splitIndex),
      str.SubString(splitIndex, myString.Lenght) };
  }
}

(It always takes me a few minutes to rewrite this sort of thing whenever I need it, so I thought I'd make a question for it)

If you need this frequently, you could convert your preferred way of doing it into an extension method. Based on Teoman Soygul's suggestion:

public static class StringExtensions
{
  public static string[] TwoParts(this String str, char splitCharacter)
  {
    int splitIndex = str.IndexOf(splitCharacter);
    if(splitIndex == -1)
      throw new ArgumentException("Split character not found.");

    return new string[] {
      str.SubString(0, splitIndex),
      str.SubString(splitIndex, myString.Lenght) };
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文