Solved

Splitting String (Square Brackets) (C#)

Posted on 2014-09-25
4
705 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 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 34

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 34

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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