substring

Private Sub Error(ByVal, ByVal Code As Integer)
  Call ModuleError(pRoot, pRoot.Scan, "Scan", Code)
End Sub
Private Sub Finish(ByValue as String)
    On Error GoTo Scan_Issue
    Dim p1 As IASLib.IAS_RECORD_1
    Dim p As IAS_RECORD_0
    if else

    Next  p
Exit Sub
Scan_Issue:
Call RunTimeError(p, "Scan")
End Sub





Question:

Each section begins with "Private Sub" and
ends with "End Sub".

when i read this below file...how can i store each section in separate string.

String 1 : Private Sub Error(ByVal, ByVal Code As Integer)
             Call ModuleError(pRoot, pRoot.Scan, "Scan", Code)
         End Sub
String 2 : Private Sub Finish(ByValue as String)
          On Error GoTo Scan_Issue
          Dim p1 As IASLib.IAS_RECORD_1
          Dim p As IAS_RECORD_0
          if else

          Next  p
      Exit Sub
      Scan_Issue:
      Call RunTimeError(p, "Scan")
      End Sub

similarly i have many of these strings...
shragiAsked:
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.

radcaesarCommented:
Use SPLIT() method

Split the strings on ":"

Store the even array values (which is having your code) in another file.
morguloCommented:
int startIndex = 0;
int endIndex = 0;
List<string> result = new List<string>();
while((endIndex = str.IndexOf("End Sub", startIndex)) > 0)
{
   endIndex += "End Sub".Length;
   string arg = str.Substring(startIndex, endIndex - startIndex);
   result.Add(arg);
   startIndex = endIndex + 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
HainKurtSr. System AnalystCommented:
split it with "End Sub"

then add "End Sub" to each element :)

string () sp = str.split("End Sub");
for  each s in sp s = s + "End Sub";

(I am not c# guy, it may have syntax errors :)
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, this will copy every VB Sub - End Sub code block into an string array, see the example:
// Your string to be matched.
string myVbCode = @"Private Sub Error(ByVal, ByVal Code As Integer) Call ModuleError(pRoot, pRoot.Scan, ""Scan"", Code) End Sub Private Sub Finish(ByValue as String)    On Error GoTo Scan_Issue     Dim p1 As IASLib.IAS_RECORD_1    Dim p As IAS_RECORD_0    if else     Next  p Exit Sub Scan_Issue: Call RunTimeError(p, ""Scan"") End Sub";
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(myVbCode, @"Private\s+Sub(?:.|\n)*?End\s+Sub", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// Copy matches to a string array for later use.
string[] lines = new string[matches.Count];
for (int i = 0; i < matches.Count; i++)
    lines[i] = matches[i].Value;

Open in new window


By following your explanation, in this example, lines[0] is "String 1" and lines[1] is "String 2".
Todd GerbertIT ConsultantCommented:
I would use a slightly different Regular Expression than above, primarily to allow for a "Public Sub", "Protected Sub", etc.:
(Public|Protected|Friend|ProtectedFriend|Private)\s+Sub\b(.*?)\bEnd\s+Sub

The \s+ matches one or more whitespace characters, \b matches a word boundary, .*? matches any character zero or more times, the | means "or", everything else matches itself.  In the code below "RegexOptions.Singleline" is important so that the "." in the regular expression will match any character (otherwise the "." matches any character except a newline).

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
	static void Main(string[] args)
	{
		string regexPattern = 
			@"(Public|Protected|Friend|ProtectedFriend|Private)\s+Sub\b(.*?)\bEnd Sub";

		Regex regex = new Regex(regexPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);

		List<string> subsList = new List<string>();

		MatchCollection matches = regex.Matches(testInput);

		foreach(Match m in matches)
			subsList.Add(m.Value);
	}

	static string testInput = @"Private Sub Error(ByVal, ByVal Code As Integer)
  Call ModuleError(pRoot, pRoot.Scan, ""Scan"", Code)
End Sub
Private Sub Finish(ByValue as String)
    On Error GoTo Scan_Issue
    Dim p1 As IASLib.IAS_RECORD_1
    Dim p As IAS_RECORD_0
    if else

    Next  p
Exit Sub
Scan_Issue:
Call RunTimeError(p, ""Scan"")
End Sub";
}

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.