My stri开发者_如何学运维ngs 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.
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.
You can use Regex and named groups.
string resultString = "New York (New York)";
Regex regexObj = new Regex(@"(?<City>[^(]*)\((?<PR>[^)]*)\)",RegexOptions.IgnoreCase);
var match = regexObj.Match(subjectString);
if (match.Success)
{
string city = match.Groups["City"].Value.Trim();
string province = match.Groups["PR"].Value;
}
Regex explanation
(?<City>[^(]*)\((?<PR>[^)]*)\)
:
(?<City>XXX)
defines a group named City[^(]*
matches any character that is not a(
between 0 and unlimited times
\(
matches a(
one time(?<PR>XXX)
defines a group named PR[^)]*
matches any character that is not a)
between 0 and unlimited times
\)
matches a)
one time
Regex?
You can divide your pattern into capture groups and go from there.
(.*)(\(.*\))
That will capture the city in the first group and your PR/Province part in the second.
Edit: madgnomes regex removes the paratheneses from the match, even if the Regex is getting scary at this point :)
You have a couple of methods on string
one of them being Split.
Example
var s = "City (PR)";
var words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
That would print:
City
(PR)
You can also do it like this
var words = s.Split(' ');
var city = words[0];
var pr = words[1];
You can just remove the paranthesis with other string methods if you like.
One might argue that this is not the best way to do it. It's simple and give's you want you want. As others suggested, regex is probably the "cleanest" way to do it.
Note
If you want to support cities with spaces in the name, this method won't work out that well for you. I would then suggest one of the other answers posted.
If the province is always in parenthesis, and the city name won't have any parenthesis, you can use the Split function.
string foo = "City (PR)";
string[] bar = foo.Split('(');
The bar variable will have the city in the first column of the array, and the province in the second (although you'll want to remove the right parenthesis).
Any chance you can modify the DB design to have a different field to hold whether it's a province or a state? This is a horrible design, and in order to get around it you definitely need to write some ugly code... Something along the lines of
if(CityAndProvinceOrStateIndicator.Contains(" (PR)"))
{
// This is a province.
// Get the city name only
string CityNameOnly = CityAndProvinceOrStateIndicator.Replace(" (PR)", "");
}
elseif...
Although excellent parsing options have been mentioned, I'd just like to suggest that you consider going to the source and modifying your database query and possibly even the database itself.
Obviously that's not always possible, which is probably why you're asking the question in the way you are, but the fact that you're receiving data in this way implies to me that the data needs to be structured or obtained differently.
Let the following SQL SELECT statement do all the work for you:
SELECT [Name], [Address],
LTRIM(RTRIM(LEFT([CityProv],CHARINDEX('(',[CityProv])-1))) as City,
LTRIM(RTRIM(SUBSTRING( [CityProv],
CHARINDEX('(',[CityProv]) + 1,
CHARINDEX(')',[CityProv]) - CHARINDEX('(',[CityProv]) - 1))) as Prov,
[PostalCode]
FROM Stackoverflow
Here is the dataset I created for testing:
Joel,1 Software St,NewYork (NY),12345
Jeff,321 Horror Lane,San somewhere ( CAL),90210
Zamboni,888 Wpf Rd,Vancouver (Britsh Columbia ),V0S1A0
Bill,7 Main St,Vista( WA ),77777
精彩评论