【问题标题】:Extract string between nth character and another nth character提取第n个字符和另一个第n个字符之间的字符串
【发布时间】:2025-12-12 21:00:02
【问题描述】:

当我尝试将游戏的整个地图加载到内存中时,它变得比我想要的要大一些。我正在尝试将地图分成几块。我的游戏地图是逗号分隔的。地图字符串的示例部分:“0, 0, 45, 32, 3, 3, 0, 0”。

目前我正在使用以下方法,但需要 9 秒(我的地图很大)。

String[] mapArr = map.split(", ");
short[] groundLayer = new short[chunkWidth * chunkHeight];
//code that fills in the groundLayer array

如果玩家在一个方向上走得太远,在游戏中等待 9 秒将不起作用。

我的想法是做一些事情,将“映射字符串”中的子字符串从逗号 (int firstComma) 转换为逗号 (int lastComma)。

firstComma = characterX + (characterY * mapWidth);
lastComma = firstComma + (chunkWidth * chunkHeight);

然后我将只拆分 (", ") 生成的子字符串。这是一个好主意吗?

做这样的事情最有效的方法是什么?子字符串,正则表达式,indexOf,有什么不同吗?任何帮助将不胜感激。


编辑在下面提供更多上下文:

我的地图由多个图层组成,我使用“平铺”来绘制/导出它们。这是我从文件中读取并将它们保存到短数组中的方法。我没有拆分整个地图字符串,而是尝试仅从字符 X 拆分为字符 Y。

try {
    String map = readFile("res/images/tiles/MyFirstMap-building-p.json");
    String[] strArr = map.split(", ");

    buildingLayer = new short[chunkWidth * chunkHeight];
    short arrayIndex = 0;
    for(short y = 0; y < chunkHeight; y++) {
        for(short x = 0; x < chunkWidth; x++) {
            //get the absolute position of the cell
            short cellX = (short) (characterX + x - chunkWidth / 2);
            short cellY = (short) (characterY + y - chunkHeight / 2);
            if(cellX >= 0 && cellX < mapWidth && cellY >= 0 && cellY < mapHeight) { //within bounds
                buildingLayer[arrayIndex] = Short.parseShort(strArr[cellX + (cellY * mapWidth)]);
            } else { //out of bounds, put down a placeholder
                buildingLayer[arrayIndex] = 0;
            }
            arrayIndex++;
        }
    }
} catch (IOException e) {
    logger.fatal("ReadMapFile(building)", e);
    JOptionPane.showMessageDialog(theDesktop, getStringChecked("message_file_locks") + "\n\n" + e.getMessage(), getStringChecked("message_error"), JOptionPane.ERROR_MESSAGE);
    System.exit(1);
}


private static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    }
    finally {
        stream.close();
    }
}

【问题讨论】:

  • 随机访问文件怎么样?您将计算其中每个块的位置并读取它,无需将其全部加载到内存中
  • 如果你存储非常大的数据,为什么要将它存储为字符串?如果您为地图创建类似二进制格式的东西,一切都会变得更有效率。这样,您只需存储您真正需要的数据(并计算数据) - 而不是不必要的字符串开销。
  • 嗯,它是以下格式.. 0, 0, 3, 0, 2,... 我可以直接将它提取为一个短数组,使用拆分吗?
  • 由于不知道更好的方法,我假设...
  • 拧字符串,将其存储为二进制。将其加载为字符串并不是一个非常聪明的主意。你需要为它制作一些编辑器,但如果地图很大,你无论如何都需要

标签: java substring


【解决方案1】:

这是我采用的解决方案(为简单起见,我删除了许多循环逻辑)。感谢@Elliott Frisch 在 cmets 中的帮助。

private static short[] scanMapFile(String path, int[] leftChunkSides, int[] rightChunkSides) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(path));
    scanner.useDelimiter(", ");

    short[] tmpMap = new short[chunkWidth * chunkHeight];
    int count = 0;

    while(scanner.hasNext()){
        tmpMap[count] = scanner.nextShort();    
        count++;
    }

    scanner.close();
    return tmpMap;
}

【讨论】:

    最近更新 更多