C# 2.0 函数从字符串中获取第二个单词

发布于 2024-12-09 06:24:43 字数 200 浏览 0 评论 0原文

我需要编写一个函数,它将返回第一个单词(第一个空格)之后的部分。

例如,我在 C# 2.0 中得到了以下字符串。

string str = "M000. New Delhi"

现在我想编写一个函数,如果传递了 str 则返回“New Delhi”。

请推荐!!

I need to write a function that will return me the part after the first word (first whitespace).

For example I have got below string in C# 2.0.

string str = "M000. New Delhi"

Now I want to write a function which return "New Delhi" if str is passed.

Please suggest!!

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

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

发布评论

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

评论(7

橘香 2024-12-16 06:24:43

获取第一个空格之后的字符串部分:

string city = str.Substring(str.IndexOf(' ') + 1);

即使字符串中碰巧没有空格,这也会给出结果。在这种情况下,IndexOf 方法将返回 -1,因此代码将返回整个字符串。如果在这种情况下您想要一个空字符串或异常,那么您将首先将索引获取到变量中,以便您可以检查该值:

string city;
int index = str.IndexOf(' ');
if (index == -1) {
  throw new ArgumentException("Unable to find a space in the string.");
} else {
  city = str.Substring(index + 1);
}

To get the part of the string after the first space:

string city = str.Substring(str.IndexOf(' ') + 1);

This will also give a result even if there happens to be no space in the string. The IndexOf method will return -1 in that case, so the code will return the entire string. If you would want an empty string or an exception in that case, then you would get the index into a variable first so that you can check the value:

string city;
int index = str.IndexOf(' ');
if (index == -1) {
  throw new ArgumentException("Unable to find a space in the string.");
} else {
  city = str.Substring(index + 1);
}
早乙女 2024-12-16 06:24:43
string WhichIsBestDelhi(String str)
{
  return "New Delhi";
}

无论传递什么字符串,这都有返回“New Delhi”的额外好处!

string WhichIsBestDelhi(String str)
{
  return "New Delhi";
}

This has the added benefit of returning "New Delhi" no matter what string is passed!

回忆凄美了谁 2024-12-16 06:24:43
 string  s = str.Split(new char[]{'.'})[1].Trim();
 string  s = str.Split(new char[]{'.'})[1].Trim();
当梦初醒 2024-12-16 06:24:43

我想编写一个函数,如果传递 str,则返回“New Delhi”。

干得好。

public string NewDehliFunction(string str)
{
    if (str == "M000. New Delhi")
        return "New Delhi";
    else
        return "?"; // what happens here???
}

但最大的问题是“当它传递其他东西时,你希望它做什么?”

I want to write a function which return "New Delhi" if str is passed.

Here you go.

public string NewDehliFunction(string str)
{
    if (str == "M000. New Delhi")
        return "New Delhi";
    else
        return "?"; // what happens here???
}

But the big question is "What do you want it to do when it is passed something else?"

合约呢 2024-12-16 06:24:43
  • 步骤 1:获取“ ”(空格)的第一个索引(对于您的字符串为 5)
  • 步骤 2:将您在步骤 1 中找到的数字增加 1 (对于您的情况为 6)< /em>
  • 第 3 步:获取给定字符串的起始索引为的子字符串
    您在第 2 步中找到的数字(针对您的案例的 str.Substring(6))

     私有静态字符串 ReturnThePartOfAStringAfterTheFirstWordAndWhiteSpace(string str)
        {
            if(str.Contains(" "))
            {
                  int indexOfFirstWhiteSpace = str.IndexOf(" ");
                  字符串剩余字符串AfterTheFirstWhiteSpace = str.Substring(indexOfFirstWhiteSpace + 1);
                  返回剩余的字符串之后的第一个空白空间;
            }
            别的 
                  返回字符串;
        }
    

您可以对任何字符串使用此方法来查找第一个空格之后的部分,这意味着第一个单词之后的部分。

  • Step 1: Take the first index of " "(whitespace) (5 for your string)
  • Step 2: Increment the number you found in Step 1, by 1 (6 for your case)
  • Step 3: Take the substring of the given string with start index of
    the number you found in Step 2 (str.Substring(6) for your case)

        private static string ReturnThePartOfAStringAfterTheFirstWordAndWhiteSpace(string str)
        {
            if(str.Contains(" "))
            {
                  int indexOfFirstWhiteSpace = str.IndexOf(" ");
                  string remainingStringAfterTheFirstWhiteSpace = str.Substring(indexOfFirstWhiteSpace + 1);
                  return remainingStringAfterTheFirstWhiteSpace;
            }
            else 
                  return str;
        }
    

You can use this method for any string to find the part after the first whitespace, which means the part after the first word.

早茶月光 2024-12-16 06:24:43

这一切都非常不清楚。最简单的情况:

var result = str.Substring(6);

更复杂的情况:

var result = str.Substring(str.IndexOf(". ")); // Possible off-by-one; can never remember

It's all very unclear. Simplest case:

var result = str.Substring(6);

More sophisticated:

var result = str.Substring(str.IndexOf(". ")); // Possible off-by-one; can never remember
秋凉 2024-12-16 06:24:43

// 替换“M000”。

str.Replace("M000. ",String.Empty);

// replace the "M000. "

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