Home > OS >  React Native Formatting User Address String
React Native Formatting User Address String

Time:06-24

I'm trying to format an address string in React Native.

The address string data given is 123 STREET, 123 CITY, PROVINCE 123

and I want to show only PROVINCE 123

What should I do.

Any help will be appreciated

CodePudding user response:

if the address format is consistent, you could do this:

const address = "123 STREET, 123 CITY, PROVINCE 123"
const province = address.split(', ')[2]

the split function .split(', ') turns the string into an array of substrings and use ', ' as the separator, which return an array like this

["123 STREET","123 CITY","PROVINCE 123"]

then just access the index of the province, you could do this if only the format is consistent (street and city must be provided or at least an empty string just to keep the format street-name, city-name, province-name)

  • Related