如何设置从.txt文件中读取文本的文本格式

时间:2018-01-24 22:41:57

标签: java

我正在尝试从文件中读取文本,然后使用String.format();对其进行格式化。该文件的内容如下所示。

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">

    <script src="../plugin/codemirror/lib/codemirror.js"></script>
    <link rel="stylesheet" href="../plugin/codemirror/lib/codemirror.css">
    <script src="../plugin/codemirror/mode/%1s/%1s.js"></script>
</head>

<body>
    <script>
        const editor = CodeMirror(document.body, {
            lineNumbers: true,
            mode: "%1s",
        });

        function changeEditorSize(width, height){
            editor.setSize(width, height);
        }
    </script>
</body>
</html>

我将内容存储在名为“content”的变量中,然后尝试以下操作(“content”的值与文件内容相同)。

String.format(content, "javascript");

然后,当“%1s%”显然在字符串中时,我收到错误“java.util.MissingFormatArgumentException:Format specifier'%1s'”。有谁知道为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

String.format使用$指定参数索引。

您需要%1$s

相关问题