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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With