Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I have a string like this:
C#
string abc = "  /*some 10 lines of code */   
    // Program Begin
    {
          //Some Code Here
          //Some Code Here
          //Some Code Here
          //Some Code Here
          //Some Code Here
    }
    // Program End
    //some liens of code
    //some liens of code";

I want only the code between "// Program Begin" and "// Program End".

How to do this.
I searched in Google and not get any effective result.

Please Help

Thanks in advance.
Posted
Updated 10-Oct-12 1:43am
v4
Comments
Andy411 10-Oct-12 7:43am    
Do you have only these two parenthesis { and } ? Then search for the Index of both and copy the part between the indexes.
Mac12334 10-Oct-12 7:46am    
No there may be more in the code
Andy411 10-Oct-12 7:50am    
You need something unique at the opening parentesis of the code block you are interested in to find it.
sahabiswarup 10-Oct-12 7:49am    
question is not clear.

1 solution

You can find the index of your start element and end element by using System.String.IndexOf("// Program Begin") and
System.String.IndexOf("// Program End")
details here[^]
Then you can do a System.String.Substring(firstIndex, numberOfcharacters)
details here[^]
The numberOfCharacters can be retrieved by subtracting both indexes in the right order.

Good luck!
 
Share this answer
 
Comments
Mac12334 10-Oct-12 9:49am    
Thanks

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