[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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...
0
shragi
Asked:
shragi
4 Solutions
 
radcaesarCommented:
Use SPLIT() method

Split the strings on ":"

Store the even array values (which is having your code) in another file.
0
 
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

0
 
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 :)
0
 
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".
0
 
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now