我可以使用indexof()在for循环中返回两个不同的值吗?

nkoocmlb  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(391)
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”和“|”的索引 String input 在for循环的每次迭代中。 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);
    }
}
yruzcnhs

yruzcnhs1#

您的问题是,对于每个索引“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));
}
qaxu7uf2

qaxu7uf22#

通过使用java regex 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

检查 Pattern 以及 Matcher 去了解他们。您还可以查看oracle的课程:正则表达式教程。

相关问题