Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Splitting String (Square Brackets) (C#)

Posted on 2014-09-25
4
Medium Priority
?
826 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 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 35

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 35

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

URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

660 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