'VS Code Extension API - Replace a String in the document?

const textEditor = vscode.window.activeTextEditor;
        
        if (!textEditor) {
            return;  // No open text editor
        }

        for(var i=0;i<textEditor.document.lineCount;i++)
        {
            var textLine = textEditor.document.lineAt(i);
            for(var j=textLine.range.start.character; j<=textLine.range.end.character; j++)
            {
                var startposition = new vscode.Position(i,j);
                var endposition = new vscode.Position(i,j+1);
                var range = new vscode.Range(startposition,endposition);
                var text = textEditor.document.getText(range);
                if(text === "\'"){
                    textEditor.edit(editBuilder => editBuilder.replace(range,"\""));
                }
            }
        }

I need to replace all the single quotes with double quotes. But what happens is the textEditor.edit(editBuilder => editBuilder.replace(range,"\"")); only replaces the 1st occurence. I need to replace all of the occurence in the document.



Solution 1:[1]

    let doc = vscode.window.activeTextEditor.document;
    let editor = vscode.window.activeTextEditor;
    var j=0;

    editor.edit(editBuilder => {
        for(var i=0;i<doc.lineCount;i++)
        {
            var line = doc.lineAt(i);
            for(j=0;j<line.range.end.character;j++)
            {
                var startposition = new vscode.Position(i,j);
                var endingposition = new vscode.Position(i,j+1);
                var range = new vscode.Range(startposition,endingposition);
                var charac = editor.document.getText(range);
                if(charac == '\'')
                {
                    editBuilder.replace(range,'\"');
                    console.log(startposition);
                }

            }
        }
    })

`

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rengarajan