Splitting string {}

Ok here is the challenge.  Say I have string that looks like this:

this is a name string and can be of any length {12345}

I need a mechanism to remove what’s in between the curly brackets, so in this example I would get this:

12345

The contents of the curly brackets could any length (but will always be a number)
andyw27Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
There are multiple ways to do this:

string original = "{12345}";
string result = original.Substring(1, original.IndexOf('}') - 1);

// OR
result = original.Replace("{", string.Empty).Replace("}", string.Empty);

// OR
result = System.Text.RegularExpressions.Regex.Match(original, "\d+").Value;

Open in new window


...and probably several other ways.
draekCommented:
You could do it like this or by using regex.

string str = "{12345}";
string result = str.Split(new char[] { '{', '}'})[1];

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
String s = "this is a name string and can be of any length {12345}";
            int i = s.IndexOf('{');
            string res = s.Substring(i + 1, s.IndexOf('}') - i - 1);
res contains 12345
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

andyw27Author Commented:
Cool, this one works well thanks.
AndyAinscowFreelance programmer / ConsultantCommented:
You are expected to accept the comment(s) that solved your problem to close the question.

ps.  If the string always ends with the } then this is a simplified version of my earlier suggestion:
            String s = "this is a name string and can be of any length {12345}";
            string res = s.Substring(s.IndexOf('{') + 1).TrimEnd('}');
YZlatCommented:
I think the easiest way is to use Regex.

You can use the following Regex expression:  
Regex regex = new Regex(@"{.*?}");
	Match match = regex.Match("{12345}");
	if (match.Success)
	{
	    Console.WriteLine(match.Value);
	}

Open in new window

käµfm³d 👽Commented:
@YZlat

Like the one I suggested?
YZlatCommented:
Actually you are matching on digits, mine matches on curly braces, so no, it is different
YZlatCommented:
Just noticed my regex is wrong - it does not extract the value from in between curly brackets. As I understood, you just want the value without the brackets. Here is the correct one:

Regex regex = new Regex(@"(?<={)[^}]*(?=})");
            
	Match match = regex.Match("{12345}");
	if (match.Success)
	{
	    Console.WriteLine(match.Value);
	}

Open in new window

käµfm³d 👽Commented:
Touche. However, if there are no other numeric digits in the string, then the result is the same; otherwise, yes, the braces should be included.

Admittedly, I overlooked that the text was a part of a larger string. Reading the question too quickly again  : \
AndyAinscowFreelance programmer / ConsultantCommented:
>>I think the easiest way is to use Regex.

Why?  I think for such a simple case as this regex is using a sledgehammer to crack a nut.  @"(?<={)[^}]*(?=})" looks rather complex and unless one is an absolute expert on regex is not at clear what that should do.
Thunder724Commented:
Try this.....

DIM ORGString$ = "[12345}"
DIM NEWSString$ = Mid(ORGString, 2, len(ORGString) - 1)

MessageBox.Show(NEWSString$)

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.