[SOLVED] Slice User Address

Hello,

I am trying to slice a user’s address once they authenticate into the dapp, the way metamask does it.

example address: 0x55555555555555555555555555555555555557890

slice it to show on front end : 0x555…7890

Thank you

You can use this -

Or write this function and pass in the address string–

const getEllipsisTxt = (str, n = 6) => {
  if (str) {
    return `${str.slice(0, n)}...${str.slice(str.length - n)}`;
  }
  return "";
};
1 Like

Gold, worked. thanks brother

2 Likes

You could just use .slice(-6)

    const userAddress = await window.ethereum.request({ method: 'eth_requestAccounts' });
    let userColour = '#' + userAddress[0].slice(-6);
    console.log(userColour);
1 Like