views:

822

answers:

4

Can anyone help me convert the following string ("D/6, 3RD FLOOR B- WING HANS CO-OPERATIVE SOCIETY DAWOOD BAGH J.P. ROAD CROSS ANDHERI WEST MUMBAI-400058") into the below string with (,) to start with new line using C# and asp .net.

D/6, 3RD FLOOR B- WING, 
HANS CO-OPERATIVE SOCIETY,
DAWOOD BAGH,
J.P. ROAD CROSS,
ANDHERI WEST,
MUMBAI-400058.
A: 

Mahesh there are no defined rules in your case w.r.t where to place the comma.

In case you want to place text after comma on a new line you can use:

String.Replace(",", "," + Environment.NewLine);
Rashmi Pandit
+4  A: 

There are no tells as to which character determines where the new line is.

So this is currently not possible.

You would need to have a unique character or pattern that states that a new line is required so that you could then replace using Regex.

The only way you could do it, would be if you had each street, city, postcode etc. etc. then you could create a function that would compare the string and replace new lines in between known streets, areas, cities etc. Although I'm not sure if that is what you're looking for.

ThePower
This is true. The string is too open-ended to be parsed. If there were some character to split the individual sections then it would be easy.
kjfletch
so is it tht we have to convert the necessary string with the required unique character then get the following result
ROHAN CHAVAN
There has to be a completely unique character that you have to represent a new line, otherwise each case of the character you are currently using will be replaced with a new line, which is not the desired effect that you require.
ThePower
Not really, you don't have to have "unique" characters. You just need to follow a consistent pattern that all your strings adhere to.
kjfletch
Not necessarily a single character, it can also be a set or combination of characters
Vinko Vrsalovic
Sorry I meant unique 'phrase' (combination) or single character, my bad!
ThePower
A: 

A regular expression won't be able to parse such an input semantically. You have to be able to formulate rules about what to modify.

ymihere
+1  A: 

For a generic solution, you would need a dictionary with all the Wings, apartments, colonies, lanes, streets. May be you can compile it with the help of local post office. Then you should parse the string to find the landmarks. Once this is done, you can format it accordingly.

The simple solution would be to have a delimiter set properly when the addresses are collected and later use a simple regex replacement.

Canopus