开发者

C# how to split a string backwards?

开发者 https://www.devze.com 2023-03-20 04:23 出处:网络
What i开发者_高级运维\'m trying to do is split a string backwards.Meaning right to left. string startingString = \"<span class=\\\"address\\\">Hoopeston,, IL 60942</span><br>\"

What i开发者_高级运维'm trying to do is split a string backwards. Meaning right to left.

string startingString = "<span class=\"address\">Hoopeston,, IL 60942</span><br>"

What I would do normally is this.

string[] splitStarting = startingString.Split('>');

so my splitStarting[1] would = "Hoopeston,, IL 60942</span"

then I would do

string[] splitAgain = splitStarting[1].Split('<');

so splitAgain[0] would = "Hoopeston,, IL 60942"

Now this is what I want to do, I want to split by ' ' (a space) reversed for the last 2 instances of ' '.

For example my array would come back like so:

[0]="60942"

[1]="IL"

[2] = "Hoopeston,,"

To make this even harder I only ever want the first two reverse splits, so normally I would do something like this

string[] splitCity,Zip = splitAgain[0].Split(new char[] { ' ' }, 3);

but how would you do that backwards? The reason for that is, is because it could be a two name city so an extra ' ' would break the city name.


Regular expression with named groups to make things so much simpler. No need to reverse strings. Just pluck out what you want.

var pattern = @">(?<city>.*) (?<state>.*) (?<zip>.*?)<";
var expression = new Regex(pattern);
Match m = expression .Match(startingString);
if(m.success){
    Console.WriteLine("Zip: " + m.Groups["zip"].Value);
    Console.WriteLine("State: " + m.Groups["state"].Value);
    Console.WriteLine("City: " + m.Groups["city"].Value);
}

Should give the following results:

 Found 1 match:

   1. >Las Vegas,, IL 60942< has 3 groups:
         1. Las Vegas,, (city)
         2. IL (state)
         3. 60942 (zip)

String literals for use in programs:

C#
    @">(?<city>.*) (?<state>.*) (?<zip>.*?)<"


One possible solution - not optimal but easy to code - is to reverse the string, then to split that string using the "normal" function, then to reverse each of the individual split parts.

Another possible solution is to use regular expressions instead.


I think you should do it like this:

var s = splitAgain[0];
var zipCodeStart = s.LastIndexOf(' ');
var zipCode = s.Substring(zipCodeStart + 1);
s = s.Substring(0, zipCodeStart);
var stateStart = s.LastIndexOf(' ');
var state = s.Substring(stateStart + 1);
var city = s.Substring(0, stateStart );
var result = new [] {zipCode, state, city};

Result will contain what you requested.


If Split could do everything there would be so many overloads that it would become confusing.

Don't use split, just custom code it with substrings and lastIndexOf.

  string str = "Hoopeston,, IL 60942";
  string[] parts = new string[3];
  int place = str.LastIndexOf(' ');
  parts[0] = str.Substring(place+1);
  int place2 = str.LastIndexOf(' ',place-1);
  parts[1] = str.Substring(place2 + 1, place - place2 -1);
  parts[2] = str.Substring(0, place2);


You can use a regular expression to get the three parts of the string inside the tag, and use LINQ extensions to get the strings in the right order.

Example:

string startingString = "<span class=\"address\">East St Louis,, IL 60942</span><br>";

string[] city =
  Regex.Match(startingString, @"^.+>(.+) (\S+) (\S+?)<.+$")
  .Groups.Cast<Group>().Skip(1)
  .Select(g => g.Value)
  .Reverse().ToArray();

Console.WriteLine(city[0]);
Console.WriteLine(city[1]);
Console.WriteLine(city[2]);

Output:

60942
IL
East St Louis,,


How about

using System.Linq
...
splitAgain[0].Split(' ').Reverse().ToArray()

-edit-

ok missed the last part about multi word cites, you can still use linq though:

splitAgain[0].Split(' ').Reverse().Take(2).ToArray()

would get you the

[0]="60942" 
[1]="IL"

The city would not be included here though, you could still do the whole thing in one statement but it would be a little messy:

var elements = splitAgain[0].Split(' ');
var result = elements
  .Reverse()
  .Take(2)
  .Concat( new[ ] { String.Join( " " , elements.Take( elements.Length - 2 ).ToArray( ) ) } )
  .ToArray();

So we're

  • Splitting the string,
  • Reversing it,
  • Taking the two first elements (the last two originally)
  • Then we make a new array with a single string element, and make that string from the original array of elements minus the last 2 elements (Zip and postal code)

As i said, a litle messy, but it will get you the array you want. if you dont need it to be an array of that format you could obviously simplfy the above code a little bit.

you could also do:

var result = new[ ]{ 
               elements[elements.Length - 1], //last element
               elements[elements.Length - 2], //second to last
               String.Join( " " , elements.Take( elements.Length - 2 ).ToArray( ) ) //rebuild original string - 2 last elements
             };


At first I thought you should use Array.Reverse() method, but I see now that it is the splitting on the ' ' (space) that is the issue. Your first value could have a space in it (ie "New York"), so you dont want to split on spaces.

If you know the string is only ever going to have 3 values in it, then you could use String.LastIndexOf(" ") and then use String.SubString() to trim that off and then do the same again to find the middle value and then you will be left with the first value, with or without spaces.


Was facing similar issue with audio FileName conventions.

Followed this way: String to Array conversion, reverse and split, and reverse each part back to normal.

        char[] addressInCharArray = fullAddress.ToCharArray();
        Array.Reverse(addressInCharArray);

        string[] parts = (new string(addressInCharArray)).Split(new char[] { ' ' }, 3);
        string[] subAddress = new string[parts.Length];

        int j = 0;
        foreach (string part in parts)
        {
            addressInCharArray = part.ToCharArray();
            Array.Reverse(addressInCharArray);
            subAddress[j++] = new string(addressInCharArray);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消