Solved

Splitting String (Square Brackets) (C#)

Posted on 2014-09-25
4
537 Views
Last Modified: 2014-11-18
Hello,

I have string which clook like this:
a [213515]

I need a method to split this string and remove the square bracket. With the above example this would produce two strings
a
213515

The approach would have to be able to handle different lengths
I’ve tried using a regex  snippet:

string pattern = @"(?<=\[)(.*?)(?=\])";
Match output = Regex.Match("str_value", pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);

Open in new window


But this just extracts the contents of the square brackets?
Any suggestions appreciated,
0
Comment
Question by:andyw27
4 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40343533
string s = "a [213515]";
            string[] results = s.Split('[');
            results[1] = results[1].TrimEnd(']');

results[0] is "a" and results[1] is "213515"
0
 
LVL 32

Expert Comment

by:ste5an
ID: 40343535
Your description leads me to this solution:

public static void Main()
{
    string input = "a [213515]";
    string[] result = input.Split(' ');
    foreach (string element in result)
    {                
        Console.WriteLine(element.Replace("[", "").Replace("]", ""));
    }

    Console.ReadLine();
}

Open in new window

0
 
LVL 11

Expert Comment

by:louisfr
ID: 40343545
I'd use the following regex:
(.*?) *\[(.*)\]

Open in new window

0
 
LVL 32

Expert Comment

by:sarabande
ID: 40343578
the Split functions accepts all separators as a char array:

string str = "a [123456]";
string [] tokens = str.Split(new Char [] {' ', '[', ']'} );

Open in new window


Sara
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now