Hi all, sorry if this post is a duplicate, I had a search and couldn't find what I was looking for.

In simplest terms I need to use the string split method using a sting as the seed i.e.

string[] sections = line.Split("><");

Sorry if this is a stupid question, I'm used to using PHP where I could use the explode function to achieve this but I'm a complete noob when it comes to c#

Thanks in advance for any help

Recommended Answers

All 9 Replies

String.Split contains an overloaded method that uses Strings as delimiters:

string[] sections = line.Split(new String[] {"><"}, StringSplitOptions.None);
string[] sections = line.Split(new char[] {"><"}, StringSplitOptions.RemoveEmptyEntries);
string[] sections = line.Split(new char[] {"><"}, StringSplitOptions.RemoveEmptyEntries);

The problem with this is that it would split the text "this> is < a sample" into three parts. I believe the original submitter only wanted text split when the symbols appear together (but I could be wrong).

If you dont want to split it the way Momerath showed and only want to extract the values inside the opening and closing tags then you can use a regex match:

class Program
    {
        static void Main(string[] args)
        {
            Regex reg = new Regex("<(?<tagContent>.*?)>");
            string SearchString = @"<span style=""color:Blue;"">Dim</span> input <span style=""color:Red;"">As</span> <span style=""color:Green;"">String</span> = <span style=""color:#A31515;"">""plum-pear""</span>";

            Console.WriteLine("Expression:{0}", reg);
            Console.WriteLine("String to Search: {0}", SearchString);
            Console.WriteLine();

            MatchCollection matches = reg.Matches(SearchString);
            foreach (Match m in matches)
            {
                Console.WriteLine("Match Found: {0}", m.Groups["tagContent"]);
            }

            Console.ReadKey();

        }
    }

Dear Split take only one character as a input but u put both

use
Split(">");
or
Split("<");

if Again not understand then write any example or problem i will solve for u

Another variation on what Ryshad indicated is to simply use the Regex.Split method which works the same as the single character split but allows string definition for the split markers.

This can be done by first defining a regex pattern then using that pattern in your split command as such:

Regex reg = new Regex(@"><");
string[] sections = Regex.Split(inputString, reg);

Or it can be used in the following way:

string[] sections = Regex.Split(inputString, @"><");

That is, of course, unless my brain has completely rotted and I've forgotten how to use Regex already :twisted:

Hope this helps :) I know the thread's a few days old but it's still 'unsolved' so figured maybe more info could be useful.

WHilst you can use regex.Split to match patterns that are beyond the string.split function, in this case i think you would be better using a full regex match.
Using the regex.split function Lusipher gave above would only split the string where the closing and opening tags are together. Lets look at the following string:
"<html>
<body>
<p><a href="blah.com">link text</a>"

With my code you would get:
"html"
"body"
"p"
"a href="blah.com"
"/a"

With regex.split shown above you would get:
"<html>
<body>
<p"
"a href="blah.com">link text</a>"

Because "><" only occurs once due to white space the match only occurs once.

commented: Good point :) Was just giving a general usage example though :P +1

Momerath

Thanks for that, sorry I didn't mark this as solved earlier, I had to move onto other projects and I have only just got back to this one.

normmy, you thaked momerath, so i am assuming you are using the string.split("><", StringSplitOptions.None) solution. Have you considered any of what i posted? You may have issues if you have text or whitespace between the '>' and the '<'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.