迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

C# 识别字符串是否为数字

作者:迹忆客 最近更新:2024/01/03 浏览次数:

在处理现实世界中的问题时,我们希望将输入作为字符串,而将其用作整数。为了使之成为可能,我们总是必须确认输入的字符串是否为数字。

在 C# 中,我们可以使用许多方法来识别输入字符串是否为数字。


C# 使用 Enumerable.All() 方法来识别字符串是否为数字

Enumerable.All() 方法属于 LINQ。LINQ 是 C# 的一部分,用于访问不同的数据库和数据源。我们可以修改此方法以检查字符串是否为数字。我们将把 char.IsDigit() 方法作为参数传递给 Enumerable.All() 方法。

此方法的正确语法如下:

StringName.All(char.IsDigit);

示例代码:

using System;
using System.Linq;

public class IdentifyString {
  public static void Main() {
    string number = "123456";
    if (number.All(char.IsDigit)) {
      Console.WriteLine("The Given String is a Number.");
    } else {
      Console.WriteLine("The Given String is Not a Number.");
    }
  }
}

输出:

The Given String is a Number.

C# 使用 Regex.IsMatch() 方法来识别字符串是否为数字

在 C# 中,我们可以使用正则表达式来检查各种模式。正则表达式是执行特定动作的特定模式。在 C# 中,我们使用^[0-9]+$^\d+$ 正则表达式来检查字符串是否为数字。

此方法的正确语法如下:

Regex.IsMatch(StringName, @"Expression");

示例代码:

using System;
using System.Text.RegularExpressions;

public class IdentifyString {
  public static void Main() {
    string number = "123456";
    if (Regex.IsMatch(number, @"^[0-9]+$")) {
      Console.WriteLine("The Given String is a Number.");
    } else {
      Console.WriteLine("The Given String is Not a Number.");
    }
  }
}

输出:

The Given String is a Number.

在这里,重要的一点是两个正则表达式 ^[0-9]+$^\d+$ 的功能并不相同。^[0-9]+$ 用于基本的 0-9 字符,而^\d+$ 用于 Unicode 中的十进制数字,未指定 RegexOptions.ECMAScript 的类别。例如,泰语中的 4,也被标识为数字。


C# 使用 Int32.TryParse() 方法来识别字符串是否为数字

Int32.TryParse() 方法用于将数字的字符串转换为 32 位带符号整数。如果字符串不是数字,则不会成功转换,因此此方法返回 false。

此方法的正确语法如下:

Int32.TryParse(StringName, out intvariable);

这里的 intvariable 是任何未初始化的 integer 变量。

示例代码:

using System;

public class IdentifyString {
  public static void Main() {
    int n;
    string number = "123456";
    bool result = Int32.TryParse(number, out n);
    if (result) {
      Console.WriteLine("The Given String is a Number.");
    } else {
      Console.WriteLine("The Given String is Not a Number.");
    }
  }
}

输出:

The Given String is a Number.

C# 使用 foreach 循环来识别字符串是否为数字

这是识别字符串是否为数字的最基本过程。在这个过程中,我们将使用 foreach 循环将字符串的每个字符检查为数字。

使用 foreach 循环的正确语法如下:

foreach (datatype variablename in somecollection) {
  // steps to iterate
}

示例代码:

using System;

public class IdentifyString {
  // custom method to check if a string is a number
  public static bool CustomMethod(string number) {
    foreach (char c in number) {
      if (c >= '0' && c <= '9') {
        return true;
      }
    }
    return false;
  }
  public static void Main() {
    string number = "123456";
    if (CustomMethod(number)) {
      Console.WriteLine("The Given String is a Number.");
    } else {
      Console.WriteLine("The Given String is Not a Number.");
    }
  }
}

输出:

The Given String is a Number.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C# 中发出 HTTP POST Web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求

在 C# 中调整图像大小

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

在 C# 中下载图片

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据

在 C# 中使用秒表

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。

在 C# 中获取可执行路径

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便