迹忆客 专注技术分享

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

C# 中的 CharAt

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

本教程将演示如何使用 Java 方法 charAt() 的等价函数。charAt() 是一种按位置从字符串中提取单个字符的方法。在 C# 中有几种方法可以做到这一点,我们将在下面讨论。

虽然字符串在 C# 中是一个类而不是数组,但在这种情况下,可以将字符串与数组类似地对待。就像你如何将索引器应用于数组以获取特定位置的值一样,你可以从字符串中提取一个字符。这是从字符串中提取单个字符的最简单、最有效的方法。

string sample_str = "Test";
char sample_chr = sample_str[0];

所有索引都是从零开始的,因此第一个字符可以被索引 0 拉出,第二个字符可以被 1 拉出,等等。

例子:

using System;

namespace StringArray_Example {
  class Program {
    static void Main(string[] args) {
      // Initialize the test string
      string test_str = "Test String";
      // Get the first character of the string at index 0
      char charAt = test_str[0];
      // Print the character pulled
      Console.WriteLine("The Character at Index 0 is: " + charAt);

      // Get the last character of the string
      // The index is the length of the test string minus 1
      charAt = test_str[test_str.Length - 1];
      // Print the character pulled
      Console.WriteLine("The last character of the string is: " + charAt);

      Console.ReadLine();
    }
  }
}

在上面的示例中,我们声明了一个字符串来提取字符。将字符串视为数组并按索引拉出字符,我们拉出第一个字符(索引为 0)和最后一个字符(字符串长度的索引减 1)。

请务必注意,如果你尝试提供大于或等于字符串大小的索引,你将收到 Index was outside the bounds of the array 错误。

输出:

The Character at Index 0 is: T
The last character of the string is: g

C# 中的 Substring 方法

你可以使用的另一种方法是使用 Substring() 方法,该方法根据提供的位置和要检索的字符串的长度从字符串中提取字符串。此方法速度较慢,但​​可以轻松修改为拉出多个字符。与 indexer 方法不同,它返回一个字符串而不是一个字符。

string sample_str = "Test";
string sample_chr = sample_str.Substring(index, length);

默认情况下,如果将长度留空,它将返回从索引位置到字符串末尾的所有字符。

例子:

using System;

namespace Substring_Example {
  class Program {
    static void Main(string[] args) {
      // Initialize the test string
      string test_str = "Test String";
      // Get the first character of the string at index 0
      string charAt = test_str.Substring(0, 1);
      // Print the character pulled
      Console.WriteLine("The Character at Index 0 is: " + charAt);

      // Get the last character of the string
      // The index is the length of the test string minus 1
      charAt = test_str.Substring(test_str.Length - 1, 1);
      // Print the character pulled
      Console.WriteLine("The last character of the string is: " + charAt);

      Console.ReadLine();
    }
  }
}

在上面的示例中,我们声明了一个字符串来提取字符。使用 Substring() 方法,我们从索引 0 和长度为 1 的子字符串中提取子字符串。在将结果打印到控制台后,我们还使用相同的方法检索字符串的最后一个字符。字符串索引器和子字符串方法的输出是相同的。

输出:

The Character at Index 0 is: T
The last character of the string is: g

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便