可以使用indexOfsubstring方法来截取字符串。

首先,使用indexOf方法找到字符串中#的位置,并保存在变量index1中:

int index1 = str.indexOf("#");

然后,使用indexOf方法找到字符串中的位置,并保存在变量index2中:

int index2 = str.indexOf("楼");

接着,使用indexOf方法找到字符串中F的位置,并保存在变量index3中:

int index3 = str.indexOf("F");

最后,使用substring方法截取字符串中#之前的部分和F之间的部分:

String result = str.substring(0, index1) + str.substring(index2 + 1, index3);

完整代码示例:

public class Main {
    public static void main(String[] args) {
        String str = "6#楼5F晾晒平台";
        int index1 = str.indexOf("#");
        int index2 = str.indexOf("楼");
        int index3 = str.indexOf("F");
        String result = str.substring(0, index1) + str.substring(index2 + 1, index3);
        System.out.println(result);
    }
}

输出结果为:

6#5
``
java 截取字符串 6#楼5F晾晒平台 获取#号之前和 楼与F之间的字符

原文地址: http://www.cveoy.top/t/topic/iufe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录