Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# 判断字符串是否为数字

作者:傲骨依旧   发布日期:2025-06-27   浏览:88

using System;

class Program
{
    static void Main()
    {
        string input = "12345";
        bool isNumeric = IsNumeric(input);
        Console.WriteLine(isNumeric); // 输出: True

        input = "abc123";
        isNumeric = IsNumeric(input);
        Console.WriteLine(isNumeric); // 输出: False
    }

    static bool IsNumeric(string str)
    {
        // 尝试将字符串转换为双精度浮点数
        // 如果转换成功,则返回 true,否则返回 false
        double result;
        return double.TryParse(str, out result);
    }
}

解释说明:

  1. IsNumeric 方法:此方法使用 double.TryParse 方法尝试将字符串转换为双精度浮点数。如果转换成功,则返回 true,表示该字符串是数字;否则返回 false
  2. Main 方法:在 Main 方法中,我们测试了两个字符串 "12345""abc123",并输出它们是否为数字的结果。

上一篇:c# textbox

下一篇:c#或

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c#高并发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站