7 Replies - 7335 Views - Last Post: 09 September 2016 - 12:12 PM Rate Topic: -----

#1 VIPERHlr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 05-August 11

How to split a string between two characters ?

Posted 09 September 2016 - 08:23 AM

I have a string of numbers separated by ','. If the input string is say ",139,156,65" and I only want to extract the number between two ',' so for the example the extracted numbers would be 139 and 156. If I use a delimiter it would extract the 65 which I don't want. How can I do this ?
Is This A Good Question/Topic? 0
  • +

Replies To: How to split a string between two characters ?

#2 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,255
  • Joined: 12-December 12

Re: How to split a string between two characters ?

Posted 09 September 2016 - 08:30 AM

You could find where the first, second and third commas are, and dismantle the string using these values (using Substring, for example), but it is much simpler to just split it on the comma and discard or ignore the number(s) you don't need.

You also have

String.LastIndexOf Method (String)

but, again, it is easier just to split the string.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7107
  • View blog
  • Posts: 24,133
  • Joined: 05-May 12

Re: How to split a string between two characters ?

Posted 09 September 2016 - 09:17 AM

But, by splitting, you are incurring the overhead of the memory allocation for the resulting array and the strings in the array. By using LastIndexOf() and SubString() you'll only need to get the memory hit one string allocation. Perhaps its premature optimization, but I grew up writing C/C++ code and I instinctively consider what the underlying operations would be. Perhaps I need to break that habit when dealing with C#.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,255
  • Joined: 12-December 12

Re: How to split a string between two characters ?

Posted 09 September 2016 - 09:28 AM

If I were just discarding the tail value then I'd use LastIndexOf and Substring. Because we still need to extract the first and second numbers, I'd still favour the simplicity of Split.

It would be a useful exercise though, for the OP to achieve this without Split.
Was This Post Helpful? 0
  • +
  • -

#5 VIPERHlr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 05-August 11

Re: How to split a string between two characters ?

Posted 09 September 2016 - 09:51 AM

I tried the split method but it doesnt seem to be working right

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           string numbers = "3658,156,50269,15368,23698,1236";
           string[] output = numbers.Split(',',',');
           for (int i=0; i<output.Length; i++)
            {
                textBox1.AppendText(output[i]);
                textBox1.AppendText(Environment.NewLine);
            }
        }
    }
}


Output:
Attached Image

3658 and 1236 shouldn't display
Am I using it wrong ?

This post has been edited by VIPERHlr: 09 September 2016 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,255
  • Joined: 12-December 12

Re: How to split a string between two characters ?

Posted 09 September 2016 - 10:03 AM

Yes, you are using it wrong. String.Split splits on any of the given delimiters. You've just assumed that if you supply two delimiters in sequence it will apply them separately in sequence.

Guessing and hoping isn't the way to progress. You need to test and understand the methods you wish to use.
Was This Post Helpful? 0
  • +
  • -

#7 VIPERHlr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 111
  • Joined: 05-August 11

Re: How to split a string between two characters ?

Posted 09 September 2016 - 11:32 AM

Thanks, I solved it using a combination of indexof,lastindexof,substring and split

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string numbers = "3658,156,50269,15368,23698,1236";
            int index1 = numbers.IndexOf(",");
            int index2 = numbers.LastIndexOf(",");

            String sub = numbers.Substring(index1+1,(index2-index1)-1);

            string[] output = sub.Split(',');

            for(int i=0; i<output.Length; i++)
            {
                textBox1.AppendText(output[i]);
                textBox1.AppendText(Environment.NewLine);

            }
        }
    }
}



Output:
Attached Image
Was This Post Helpful? 1
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How to split a string between two characters ?

Posted 09 September 2016 - 12:12 PM

Regular Expressions are also an option:

var regex = new Regex(@"(?=,(\d+),)", RegexOptions.Compiled);
var testString = "3658,156,50269,15368,23698,1236";
var matches = regex.Matches(testString);
var numbers = matches.Cast<Match>().Select(m => m.Groups[1].Value).ToList();


Of course...it's not even as performant as your code. Even with a compiled regex that isn't recreated for each request, your method will run an order of magnitude faster for your given test string (though they're both fast; on my machine the average of 1000 tries, yours averages 0.00047 milliseconds and the regex averages 0.00744 milliseconds). So stick with what you've got.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1