1

I would like to separate the string "ICMS_MG-PR-RJ-RS-SC" and need to exclude the "ICMS_" add the other values excluding the "-" to the list of string.

List<string> states=new List<string>(); 

The "states" should contain the values as: MG,PR,RJ,RS,SC

3
  • Not that important question to give 2 up votes Commented Jun 10, 2013 at 9:38
  • @PawanNogariya: especially because he didn't show what he has tried. Commented Jun 10, 2013 at 9:49
  • @TimSchmelter - Exactly! Commented Jun 10, 2013 at 10:04

3 Answers 3

5

Replace prefix with empty string, and split rest by - symbol:

var text = "ICMS_MG-PR-RJ-RS-SC"; var states = text.Replace("ICMS_", "").Split('-').ToList(); 
Sign up to request clarification or add additional context in comments.

Comments

1
var states = text.Split('_', '-').Skip(1).ToList(); 

Comments

0

In addition to lazyberezovsky's answer, you can also do this:

var input = "ICMS_MG-PR-RJ-RS-SC"; var states = input.Split('_')[1].Split('-').ToList(); 

Or this

var states = input.Split('_', '-').Skip(1).ToList(); 

Or this

var states = input.Substring(input.IndexOf('_') + 1).Split('-').ToList(); 

Note, however, unless you really need to be able to access each element by index, there's probably no need to call ToList. If you can settle for an IEnumerable<string>, you probably should.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.