简体   繁体   English

如何检查特定索引处的字符是否与数组中的项目匹配

[英]How to check if a character at a certian index matches an item within an array

I'm trying to take a string as an input (around 5 - 7 characters), iterate through each character and check whether or not it matches a character from an array.我正在尝试将字符串作为输入(大约 5 - 7 个字符),遍历每个字符并检查它是否与数组中的字符匹配。 For example:例如:

static String Registration(String reg) {
    int[] numbers = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9,};
    if (reg.length() >= 7) {
        for (int i = 0; i < reg.length(); i++) {
            reg.indexOf(numbers[i]);
        }
    }
}

The above is clearly wrong.以上显然是错误的。 However, I hope it illustrates what I'm trying to do.但是,我希望它说明了我正在尝试做的事情。 For example, if the string was "H3llo", I would want to check each character in the string and see if it matches with any items in the array.例如,如果字符串是“H3llo”,我想检查字符串中的每个字符,看看它是否与数组中的任何项目匹配。 If it does, I would like to store the index as an int.如果是这样,我想将索引存储为 int。

This is probably not an efficient way of doing it, but it's the only way I can think of with my current level of learning.这可能不是一种有效的方法,但这是我目前的学习水平能想到的唯一方法。

there are several problems with your code here:您的代码在这里有几个问题:

  • your numbers array is of type int, however you want to compare the characters.您的numbers数组是 int 类型,但是您想比较字符。 use char[] instead and use chars in your array instead of ints like this: '0', '1', ...改用 char[] 并在数组中使用字符而不是像这样的整数: '0', '1', ...
  • indexOf is not a existing method for arrays, instead use a for loop to iterate through all the entries of the array and compare them by hand indexOf 不是 arrays 的现有方法,而是使用 for 循环遍历数组的所有条目并手动比较它们
  • your function claims to return a String public String Registration but you want to return an int.您的 function 声称返回一个字符串public String Registration ,但您想返回一个 int。 change this to public int Registration .将此更改为public int Registration
  • You have no return in your function.您的 function 没有回报。 you have to use the keyword return in your function if your function should return something.如果你的 function 应该返回一些东西,你必须在你的 function 中使用关键字return

A possible solution may look like this:一个可能的解决方案可能如下所示:

public static int Registration(String reg) {
  char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7' , '8', '9',}; // use a char array char[] and add numbers as chars to array.
  if (reg.length() >= 7) {
    for (int i = 0; i < reg.length(); i++) {
      char character = reg.charAt(i);
      for(char number : numbers)
      {
        if(number == character)
        {
          return i; // found a matching character, return the index of the matching character
        }
      }
    }
  }
  // return -1 for the case that we find nothing.
  return -1;
}

please try to understand what happens here and do not just copy and paste the solution.请尝试了解此处发生的情况,不要只是复制和粘贴解决方案。

Note: the thing you wish to do can be easily implemented with RegEx, however, this may be too hard for you at your current stage.注意:你想做的事情可以很容易地用 RegEx 实现,但是,这对你目前的阶段来说可能太难了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM