我可以在 for 循环中使用 indexOf() 来 return 两个不同的值吗?

Can I use the indexOf() to return two different values in a for-loop?

 String input = "B||B|B";       
 char bridgeMarker = 'B';
 char spaceMarker = '|';

 for(int i = 0; i < input.length(); i++)
 {
    int posB = input.indexOf(bridgeMarker, i);
    System.out.println("bridge index: " + posB);
    
    int posSp = input.indexOf(spaceMarker, i);
    System.out.println("space index: " + posSp);

    if(posB > 0 && posB < input.length() && posSp > 0 && posSp < input.length())
    {
        //do something
    }
 }

我正在尝试查找 'B' 和“|”每次出现的索引在 for 循环的每次迭代中 String input

int posSp = input.indexOf(spaceMarker, i);int posB = input.indexOf(bridgeMarker, i); 分别工作正常,但是当我同时使用它们时,我得到令人困惑的输出:

bridge index: 1
space index: 0
bridge index: 1
plot index: 2
bridge index: 3
plot index: -1
bridge index: 3
space index: -1
bridge index: 4
space index: -1
bridge index: -1
space index: -1
bridge index: -1
space index: -1

我已经做了好几个小时了,我很迷茫,所以如果有人能给我任何建议,我将不胜感激!

这就是我计划修改字符串值的方式:

if(input.charAt(posSp - 1) == input.charAt(posB) || input.charAt(posSp + 1) == input.charAt(posB))
{
    if(input.charAt(posB - 1) != 'R' || input.charAt(posB + 1) != 'R') 
    {
        input = input.replace(spaceMarker, ropeMarker);
        System.out.println(input);
    }
}

您可以使用 Java 正则表达式 API:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String input = "B||B|B";
        Pattern pattern = Pattern.compile("[B\|]");
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            System.out.println(matcher.group() + " => " + matcher.start());
        }
    }
}

输出:

B => 0
| => 1
| => 2
B => 3
| => 4
B => 5

查看 Oracle 的 Pattern and Matcher to learn more about them. You can also check Lesson: Regular Expressions 教程。

您的问题是,对于每个索引 'i',您正在检查从 i 开始到字符串末尾的子字符串。看看https://www.w3schools.com/java/ref_string_indexof.asp

使用这样的东西

String input = "B||B|B";
char bridgeMarker = 'B';
char spaceMarker = '|';

for (int i = 0; i < input.length(); i++) {
    if (input.charAt(i) == bridgeMarker) {
        System.out.println("B at index " + i);
    } else if (input.charAt(i) == spaceMarker) {
        System.out.println("| at index " + i);
    } else {
        System.out.println("Neither B nor | wos found");
    }
}

编辑 要在程序中使用这些值,请将它们存储在列表

String input = "B||B|B";
char bridgeMarker = 'B';
char spaceMarker = '|';

List<Integer> bridgeMarkers = new ArrayList<>();
List<Integer> spaceMarkers = new ArrayList<>();

for (int i = 0; i < input.length(); i++) {
    if (input.charAt(i) == bridgeMarker) {
        bridgeMarkers.add(i);
        System.out.println("B at index " + i);
    } else if (input.charAt(i) == spaceMarker) {
        spaceMarkers.add(i);
        System.out.println("| at index " + i);
    } else {
        System.out.println("Neither B nor | wos found");
    }
}

System.out.println("bridgeMarkers: "+Arrays.toString(bridgeMarkers.toArray()));
System.out.println("spaceMarkers: "+Arrays.toString(spaceMarkers.toArray()));

// how to use them
for (int i = 0; i < bridgeMarkers.size(); i++) {
    System.out.println("bridgeMarker nr " + i + " is at index " + bridgeMarkers.get(i));
}

for (int i = 0; i < spaceMarkers.size(); i++) {
    System.out.println("spaceMarker nr " + i + " is at index " + spaceMarkers.get(i));
}