【问题标题】:Similar function charAt of Java in C# [duplicate]C#中Java的类似函数charAt [重复]
【发布时间】:2016-10-20 08:07:01
【问题描述】:

我有 Java 代码

string[] elementMath = { 东西}

elementMath[i].charAt(0);

在 C# 中,我尝试过

elementMatn[i][0];

但它会给我错误

【问题讨论】:

  • type 是什么元素数学
  • 对象引用为空
  • elementMath[i] 的数据类型是什么?
  • 是字符串数组
  • 你得到什么错误?

标签: c#


【解决方案1】:

您的数组初始化可能有问题。 这是一个与您的语法相同的工作示例:

  string[] elementMath = new [] {"aa", "bb", "cc"};
  char result = elementMath[1][0]; // result is 'b'

【讨论】:

    【解决方案2】:

    在 C# 中,字符串也是一个 char 数组。 如果元素不为空,您可以使用索引或string.ElementAt(index)

    string[] arr = string[]{ "testA", "testB", "testC" };
    char c1 = arr[0].ElementAt(4);  // c1 will be 'A'
    char c2 = arr[1][4]; // c2 will be 'B'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      相关资源
      最近更新 更多