user254883
user254883

Reputation: 605

how to remove a certain character at the end of the string in javascript

http://dren.ch/js_blowfish/

I'm experimenting with blowfishJS, but it has a bug that makes it unusable

//lets say I want to encrypt the string "house"

var bf = new Blowfish('some key');

var house = "house";
var ciphertext = bf.encrypt(house);
var plaintext = bf.decrypt(ciphertext);

console.log(plaintext); //outputs house00 instead of house

sometimes its more than 2 zeros.

This might be helpful:

All the strings im trying to encrypt end with "=" anyway. so it would be ok to remove all the zeros at the end of the "plaintext" string until the character "=" shows up.

How do I achieve this? lets say I have the string "mystring==000" and I need to remove all the zeros at the end. I have done my research on the "slice" function, the problem is that there is no certain "position" at the end since I can't know if 2,3 or x zeros will appear

Upvotes: 0

Views: 138

Answers (3)

guioconnor
guioconnor

Reputation: 414

With a regex, you can remove anything after the last = regardless of being a zero.

plaintext.replace(/[^\=]+$/, "");

In plain english that is, find a maximal substring that doesn't include an '=' and it's at the end of the string and replace it for an empty string.

Upvotes: 0

Kyle Needham
Kyle Needham

Reputation: 3389

You could use lastIndexOf() and substr from that index. for example;

var house = 'house=04543976439859df45345ffd43';

house.substr( 0, house.lastIndexOf('=') + 1 ); // => "house="

lastIndexOf will return the index of the last occurrence of the specified value (I do + 1 in this example to include the = character). Which we can then use with substr to extract all characters after that index.

Upvotes: 1

Sri Tirupathi Raju
Sri Tirupathi Raju

Reputation: 819

you can use regular expressions to do this, if you are sure the string ends with zeros and to trim till = use

plaintext.replace(/=(0)+$/g,"=");

Upvotes: 0

Related Questions