Solved

Splitting String (Square Brackets) (C#)

Posted on 2014-09-25
4
566 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 33

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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do ASP.NET and MVC work together? 4 25
Why Does This Page Not Show Up? 4 21
Tool Box 2 31
SQL Query Producing decimal places when it shouldn;t be 8 35
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

947 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

22 Experts available now in Live!

Get 1:1 Help Now