Replacing first and last characters in string (javascript)

1154
March 16, 2018, at 8:44 PM

I need to replace first and last character in random string. Why this code doesn't work?

function symbExchange(line) {  
    var tmp = line[0];  
    var str = line.replace(line[0], line[line.length-1]);  
    var str2 = str.replace(str[str.length-1], tmp);  
    return str2;
}
Answer 1

That's because the replace function returns a new string with some or all matches of a pattern replaced by a replacement.

If you need to swap characters, you can use a regex in your case (but this is not the best implementation):

function symbExchange(line) {  
    var tmp = line[0];  
    var str = line.replace(new RegExp('^' + line[0]), line[line.length-1]);  
    var str2 = str.replace(new RegExp(str[str.length-1] + '$'), tmp);  
    return str2;
}
Answer 2

It does not work because replace function will replace the first occurrence

try this :

function symbExchange(line) {  
    var first = line[0];  
    var last = line[line.length-1];  
    line[0] = last;
    line[line.length-1] = first
    return str2;
}
Answer 3

This line replaces first occurence of character that is same as last character. Not the last character itself.

var str2 = str.replace(str[str.length-1], tmp);  

possible solution is not to use replace function:

function symbExchange(line) {  
    var startCh = line[0];  
    var endCh = line[line.length-1];
    var str = line;
    str[0] = endCh;
    str[str.length-1] = startCh;
    return str;
}
Answer 4

String.Replace when used with a string argument will only replace the first occurrence of that string. You either need to use regex as an argument, or do a split 'n' join. Which one is better /faster will depend on the length of your string and number of occurrences to replace.

var src = "helloworldhello";
var dest = src.replace( /hello/g , "goodbye");
console.log(dest);

or

var src = "helloworldhello";
var dest = src.split("hello").join("goodbye");
Answer 5

Use swapping characters after converting string to array and then to string

function symbExchange(line) {  
line = line.split("");
temp=line[0]
line[0]=line[line.length-1]
line[line.length-1]=temp
return line.join("")
}
Rent Charter Buses Company
READ ALSO
How to gradually change lowpass frequency in webaudio?

How to gradually change lowpass frequency in webaudio?

I'm trying to gradually change the frequency amount of my lowpass filter, but instead of happening gradually, it happens instantly

203
PHP loop value sent to javascript

PHP loop value sent to javascript

I'm avoiding using a database for my latest project and so I am using files and foldersI list folders of a directory as buttons and each one loads a screen with a list of files within it

202
what class do moodle plugin php files extend

what class do moodle plugin php files extend

im new in the moodle plugin development world and in development world itselftrying to understand the logic behind plugin development ive faced some difficulties unterstanding how it all works

136
PHP base64_encode adding strange character at the end of string

PHP base64_encode adding strange character at the end of string

I´m using an array, which I encode into json, than into BASE64, save it on a Cookie, to be later on retrievedThe actual string is:

223