.NET 是否有相当于 C 的 atoi 函数?

发布于 2024-07-26 05:24:44 字数 324 浏览 5 评论 0原文

如果我有一个像这样的字符串:

"26 things"

我想将其转换为 26。我只想要字符串开头的整数。

如果我使用 C,我只会使用 atoi 函数。 但我似乎在.NET 中找不到任何等效的东西。

从字符串开头获取整数的最简单方法是什么?

编辑:很抱歉我的表述含糊不清。 在字符串中查找空格字符的答案在许多情况下都有效(也许甚至是我的)。 我希望 .NET 中有一个与 atoi 相当的东西。 答案也应该适用于像“26things”这样的字符串。 谢谢。

If I have a string like:

"26 things"

I want to convert it to 26. I just want the integer at the beginning of the string.

If I was using C, I'd just use the atoi function. But I can't seem to find anything equivalent in .NET.

What's the easiest way to grab the integer from the beginning of a string?

Edit: I'm sorry I was ambiguous. The answers that look for a space character in the string will work in many circumstances (perhaps even mine). I was hoping for an atoi-equivalent in .NET. The answer should also work with a string like "26things". Thanks.

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

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

发布评论

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

评论(10

回忆那么伤 2024-08-02 05:24:44

这看起来太漂亮了:

string str = "26 things";
int x = int.Parse(str.TakeWhile(ch => char.IsDigit(ch)).Aggregate("", (s, ch) => s + ch));

而且,对于任何真正想要 atoi 的人来说,这是一个无聊的解决方案:

[System.Runtime.InteropServices.DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int atoi(string str);

This looks sooo beautiful:

string str = "26 things";
int x = int.Parse(str.TakeWhile(ch => char.IsDigit(ch)).Aggregate("", (s, ch) => s + ch));

And, the boring solution for anyone who really wants atoi:

[System.Runtime.InteropServices.DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int atoi(string str);
桜花祭 2024-08-02 05:24:44

这应该可以工作(编辑以忽略字符串开头的空格)

int i = int.Parse(Regex.Match("26 things", @"^\s*(\d+)").Groups[1].Value);

如果您担心检查是否有一个值,您可以执行以下操作,如果字符串开头没有整数,则可以执行以下操作,为您提供 -1 值细绳。

Match oMatch = Regex.Match("26 things", @"^\s*(\d+)");
int i = oMatch.Success ? int.Parse(oMatch.Groups[1].Value) : -1;

This should work (edited to ignore white-space at the begining of the string)

int i = int.Parse(Regex.Match("26 things", @"^\s*(\d+)").Groups[1].Value);

If you are worried about checking if there is a value you could do the following to give you a -1 value if there is no integer at the begining of the string.

Match oMatch = Regex.Match("26 things", @"^\s*(\d+)");
int i = oMatch.Success ? int.Parse(oMatch.Groups[1].Value) : -1;
×眷恋的温暖 2024-08-02 05:24:44

您可以使用 Int32.Parse(stringVal.Substring(0, stringVal.indexOf(" "))

You could use Int32.Parse(stringVal.Substring(0, stringVal.indexOf(" "))

悟红尘 2024-08-02 05:24:44

一种方法是

string sample = "26 things";
int x = int.Parse(sample.Substring(0, sample.IndexOf(" ")));

one way would be

string sample = "26 things";
int x = int.Parse(sample.Substring(0, sample.IndexOf(" ")));
雪若未夕 2024-08-02 05:24:44

直接等效的是 int.Parse(string) 但我不完全确定这是否只需要起始数字。

The direct equivalent is int.Parse(string) but I'm not entirely sure if that will take just the starting number.

皇甫轩 2024-08-02 05:24:44

您可以在 Microsoft.VisualBasic.Conversion 命名空间中调用 Val。 在你的场景中它应该打印“26”。 我不知道它是否 100% 兼容其他场景。 这是规范的链接。

http://msdn.microsoft.com/en-我们/库/k7beh1x9%28VS.71%29.aspx

You can call Val in the Microsoft.VisualBasic.Conversion namespace. In your scenario it should print "26". I don't know if it's 100% compatible in toher scenarios though. Here's a link to the specification.

http://msdn.microsoft.com/en-us/library/k7beh1x9%28VS.71%29.aspx

梦中的蝴蝶 2024-08-02 05:24:44

如果你只想要整数

public String GetNumber (String input)
{
    String result = "";
    for (Int32 i = 0; i < input.Length; i++)
    {
        if (Char.IsNumber(input[i]))
        {
            result += input[i];
        }
        else break;
    }
    return result;
}

If you want only whole number

public String GetNumber (String input)
{
    String result = "";
    for (Int32 i = 0; i < input.Length; i++)
    {
        if (Char.IsNumber(input[i]))
        {
            result += input[i];
        }
        else break;
    }
    return result;
}
挽梦忆笙歌 2024-08-02 05:24:44

尝试这个:

int i = int.Parse("26 things".Split(new Char[] {' '})[0]);

Try this:

int i = int.Parse("26 things".Split(new Char[] {' '})[0]);
月棠 2024-08-02 05:24:44

我可以这样想:

    public static string Filter(string input, string validChars) {
        int i;
        string result = "";
        for (i = 0; i < input.Length; i++) {
            if (validChars.IndexOf(input.Substring(i, 1)) >= 0) {
                result += input.Substring(i, 1);
            } else break;
        }
        return result ;
    }

并称之为:

Filter("26 things", "0123456789");

I can think just in something like this:

    public static string Filter(string input, string validChars) {
        int i;
        string result = "";
        for (i = 0; i < input.Length; i++) {
            if (validChars.IndexOf(input.Substring(i, 1)) >= 0) {
                result += input.Substring(i, 1);
            } else break;
        }
        return result ;
    }

And call it :

Filter("26 things", "0123456789");
誰ツ都不明白 2024-08-02 05:24:44

您必须拆分字符串然后解析它:

var str = "26 things";
var count = int.Parse(str.Split(' ')[0]);

You've got to split the string and then parse it:

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