简体   繁体   English

我如何遍历字符串以找到特定字符?

[英]how do i iterate through a string of characters to find a specific character?

I am having a hard time figuring out why my code will not work. 我很难弄清楚为什么我的代码无法正常工作。 I am trying to stop the output on a specific letter, but it keeps iterating through the entire string instead. 我试图将输出停止在一个特定的字母上,但是它将继续遍历整个字符串。 This is what I have, 这就是我所拥有的

public static char stringIterator(String string) {      
    System.out.println(string);//Keep this line
    char lastChar = string.charAt(string.length() - 1);
    if (lastChar == 'M') {
        return lastChar;
    }
    else if (string.length() == 1) {
        return lastChar;
    }
    else {
        return stringIterator(string.substring(0, string.length() - 2));
    }
}

if you want to just see if it has it then you would use 如果您只想看看它是否有它,那么您将使用

string.contains('char');

if you want to traverse/iterate then 如果您想遍历/迭代则

for( int i = 0; i < string.length(); i++)
    if(string.at(i) == '#')
    { //whatever you want here
    }

You might be over-thinking this... Java has very good resources for dealing with Strings, check out the docs: Java String Documentation 您可能对此想法过度了……Java拥有很好的处理字符串的资源,请查看以下文档: Java字符串文档

if (string.contains('m')){
    //dostuff
    }

    if (string.endsWith('m')){
    //dostuff
    }

etc. 等等

As for your iteration problem, you'll have to post the rest of your code, as it looks like your Loop is Calling stringIterator(String) from somewhere outside this method. 至于您的迭代问题,您必须发布其余的代码,因为看起来您的循环正在stringIterator(String)方法外部的某个地方调用stringIterator(String) (It's not really a stringIterator if it doesn't iterate anything) (如果不进行任何迭代,它实际上不是stringIterator)

Also this last line of code: 最后这行代码:

return stringIterator(string.substring(0, string.length() - 2));

Is recursive (calls itself)... which can cause you trouble. 是递归的(调用自身)...这可能会给您带来麻烦。 There's certainly uses for recursion, finding something in a 1d array is not one of those uses. 递归肯定有用途,在一维数组中查找内容不是这些用途之一。 Assuming your loop is working properly this could be what's preventing you from stopping the output. 假设您的循环工作正常,这可能是阻止您停止输出的原因。

public String substring(int begIndex, int endIndex) - returns a new string that is a substring of the string Parameters : public String substring(int begIndex,int endIndex)-返回一个新字符串,它是字符串的子字符串。
beginIndex : the begin index, inclusive. beginIndex:开始索引(包含)。
endIndex : the end index, exclusive. endIndex:结束索引(不包括)。
(eg): String string = "NAME" (例如):字符串=“ NAME”
string.substring(0, string.length() - 2) - returns "NA" string.substring(0,string.length()-2)-返回“ NA”
string.substring(0, string.length() - 1) - returns "NAM" . string.substring(0,string.length()-1)-返回“ NAM”。 Use this, thus you will be able to subtract the last character of the string. 使用它,因此您将能够减去字符串的最后一个字符。

Iterative approach 迭代法

     public static char stringIterator(String string) {
     char lastChar = string.charAt(0);
       for(int i = string.length()-1 ;i>=0;i--) {
           System.out.println(string);//Keep this line
           lastChar = string.charAt(i);
           if (string.length() == 1 || lastChar == 'M') {
              break;
           } else {
              string = string.substring(0, i);
           }
        }
        return lastChar;
      }

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

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