Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have sample string like "Test it @Number(1,2,3,4....) that you got"

if I find @Numebr in the string I need to take the portion where @Number is start to the closing bracket ")".

C#
string data= "Anythig can be there( happened @Number(1,7, bad, nice) here)."

output: @Number(1,7, bad, nice)
Posted
Comments
Kenneth Haugland 2-Feb-15 7:20am    
https://msdn.microsoft.com/en-us/library/system.string.indexof%28v=vs.110%29.aspx
count upwards until you get to ")"?
BillWoodruff 2-Feb-15 7:31am    
Repost of http://www.codeproject.com/Questions/871873/How-can-I-parse-a-portion-of-string-replace-whole?arn=6

1. Find position "@Number"
This can be done with the IndexOf function[^]

2. Find the position of the first "("
This cane be done with the overloaded IndexOf function[^] where startindex is the value of the previous call (for @Number)

3. Find the position of the first "("
Same as 2 but startindex is the value of #2 not #1.

4. Get the portion out of the total string.
Use the Substring function[^]

With this, you should be able to get it right :-).
Hope this helps.
 
Share this answer
 
you can do this using substring function,

C#
string data= "Anythig can be there( happened @Number(1,7, bad, nice) here).";
string sub = data.Substring(data.indexOf("@Number"));
string sub1 = sub.Substring(0, data.indexOf(")")+1);


Note :

this is not tested but it will work if you play around it because this the one way to do it
 
Share this answer
 
v4
Comments
sachi Dash 2-Feb-15 7:53am    
Almost correct. Only last line should be

string sub1 = sub.Substring(0, sub.indexOf(")")+1);

Thanks a lot !
You can start by using regular expression also.

C#
Regex regex = new Regex(@"@\w+\([\w|,|\s]+\)");

Match match = regex.Match(@"Anythig can be there( happened @Number(1,7, bad, nice) here)");
if (match.Success)
{
    Console.WriteLine(match.Value);
}
 
Share this answer
 
Try regex:
using System;
using System.Text.RegularExpressions;

public class Program
{
	public static void Main()
	{
		string str = "Anythig can be there( happened @Number(1,7, bad, nice) here).";
		Regex regex = new Regex(@"@Number\([\w,\s]+\)");
		Match match = regex.Match(str);
		if (match.Success)
		{
			Console.WriteLine(match.Value);
		}
	}
}

Read more: The 30 Minute Regex Tutorial[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900