Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with manipulating some strings

Tags:

string

c#

My strings are of this kind: City (PR) from a Database, where PR stands for Province.

At the end I want two separate variables. City and PR.

I have to do this with C#. Any idea? Thanks.

like image 289
Alex Avatar asked May 20 '10 14:05

Alex


People also ask

What does string manipulation mean?

String manipulation basically refers to the process of handling and analyzing strings. It involves various operations concerned with modification and parsing of strings to use and change its data.

What are the two types of string manipulation techniques?

Shared Methods and Instance Methods You can also manipulate strings with the methods of the String class. There are two types of methods in String : shared methods and instance methods.


1 Answers

Assuming cities don't contain ( characters:

string s = "City (PR)";
int p = s.LastIndexOf("(");

string city = s.Substring(0, p).Trim();
string province = s.Substring(p + 1, s.Length - p - 2).Trim();

city is the substring from the first character up to but not including the last ( character.
province is the substring from the first character after the last ( to the end excluding the last character.

like image 153
dtb Avatar answered Sep 25 '22 06:09

dtb