E_learner
E_learner

Reputation: 3582

Omit unnecessary parts in string array

In C#, I have a string comes from a file in this format:

Type="Data"><Path.Style><Style

or maybe

Type="Program"><Rectangle.Style><Style

,etc. Now I want to only extract the Data or Program part of the Type element. For that, I used the following code:

string output;

var pair = inputKeyValue.Split('=');

if (pair[0] == "Type")
{
    output = pair[1].Trim('"');
}

But it gives me this result:

output=Data><Path.Style><Style

What I want is:

output=Data

How to do that?

Upvotes: 0

Views: 92

Answers (6)

gunr2171
gunr2171

Reputation: 17520

This code example takes an input string, splits by double quotes, and takes only the first 2 items, then joins them together to create your final string.

string input = "Type=\"Data\"><Path.Style><Style";

var parts = input
    .Split('"')
    .Take(2);

string output = string.Join("", parts); //note: .net 4 or higher

This will make output have the value:

Type=Data

If you only want output to be "Data", then do

var parts = input
    .Split('"')
    .Skip(1)
    .Take(1);

or

var output = input
    .Split('"')[1];

Upvotes: 1

Sheridan
Sheridan

Reputation: 69959

Given your specified format:

Type="Program"><Rectangle.Style><Style

It seems logical to me to include the quote mark (") when splitting the strings... then you just have to detect the end quote mark and subtract the contents. You can use LinQ to do this:

string code = "Type=\"Program\"><Rectangle.Style><Style";
string[] parts = code.Split(new string[] { "=\"" }, StringSplitOptions.None);
string[] wantedParts = parts.Where(p => p.Contains("\"")).
    Select(p => p.Substring(0, p.IndexOf("\""))).ToArray();

Upvotes: 1

Bob Vale
Bob Vale

Reputation: 18474

How about a regex?

var regex = new Regex("(?<=^Type=\").*?(?=\")");
var output = regex.Match(input).Value;

Explaination of regex

  • (?<=^Type=\") This a prefix match. Its not included in the result but will only match if the string starts with Type="

  • .*? Non greedy match. Match as many characters as you can until

  • (?=\") This is a suffix match. It's not included in the result but will only match if the next character is "

Upvotes: 1

flindeberg
flindeberg

Reputation: 5007

Maybe I missed something, but what about this:

var str = "Type=\"Program\"&gt;&lt;Rectangle.Style&gt;&lt;Style";
var splitted = str.Split('"');
var type = splitted[1]; // IE Data or Progam

But you will need some error handling as well.

Upvotes: 1

Perfect28
Perfect28

Reputation: 11317

Instead of doing many split, why don't you just use Regex :

 output = Regex.Match(pair[1].Trim('"'), "\"(\w*)\"").Value;

Upvotes: 1

AkiAki007
AkiAki007

Reputation: 429

What you can do is use a very simple regular express to parse out the bits that you want, in your case you want something that looks like this and then grab the two groups that interest you:

(Type)="(\w+)"

Which would return in groups 1 and 2 the values Type and the non-space characters contained between the double-quotes.

Upvotes: 1

Related Questions