Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

string break

I want to get substring of a string

I have the below string..

Private Sub IE_BW_CLEANUP_Error(ByVal p As IASLib.IAS_RECORD_0, ByVal Code As Integer)


from the above string i want to seperate

string 1 = IE_BW_CLEANUP
String 2= Error [the part before "(" and after last underscore]

another example

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)

String 1 = scan
String 2 = Finish



Avatar of radcaesar
radcaesar
Flag of India image

First one, Use split with _ and concat Arrays 1,2,3 and then Arrays 4,5,6.

Second, Use split with _ and again split the Array[1] with space " " and you can get "Scan" in Array[3]

Again for "Finish", Split Array[2] with "("

Note: The above method will work based on the counts of _ and Space
Avatar of shragi

ASKER

First one, Use split with _ and concat Arrays 1,2,3 and then Arrays 4,5,6.

Second, Use split with _ and again split the Array[1] with space " " and you can get "Scan" in Array[3]


can i have a general method for both... becoz i just provided an example...
i may have different examples like....

IE_BW_CLEANUP_Error(
IE_CLEANUP_Error(
IE_Error(

my approach

get the first index of "(" in the line that contians "Private Sub" and
get the substring of betwen  index of "(" and last  "_" before "("
in this way we get "Finish" and "Error"

for string 1...  how about getting the index of "Private Sub"
substring between the index of "private Sub"+ 12 and index of last "_" before first "("


this is the idea but i am unable to get last "_" before "first "(" in the line that contains "private Sub"
Avatar of Todd Gerbert
Naturally, this needs some error checking, but you get the basic idea:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
	static void Main(string[] args)
	{
		string test = "Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)";

		int endIndex = test.IndexOf('(');
		int startIndex = test.LastIndexOf('_', endIndex) + 1;

		string subString = test.Substring(startIndex, endIndex - startIndex);
	}
}

Open in new window

Then you need to get the length of the array and based on the count you should get the value.

If it has only one _ get second, if it have two _ then get third like that.
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, also you can try this:
string myVbCode = "Private Sub IE_BW_CLEANUP_Error(ByVal p As IASLib.IAS_RECORD_0, ByVal Code As Integer) Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)";
string myString1 = null, myString2 = null;
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(myVbCode, @"Sub\s+((?:.)*?)\(", RegexOptions.IgnoreCase | RegexOptions.Singleline);
foreach (System.Text.RegularExpressions.Match item in matches)
{
    string myMatchValue = item.Groups[1].Value;
    int splitPoint = myMatchValue.LastIndexOf('_');
    myString1 = splitPoint >= 0 ? myMatchValue.Substring(0, splitPoint) : myMatchValue;
    myString2 = splitPoint >= 0 ? myMatchValue.Substring(splitPoint + 1, (myMatchValue.Length - 1) - splitPoint) : string.Empty;
            
    // Show the values.
    Response.Write("String 1: " + myString1);
    Response.Write("<br/>");
    Response.Write("String 2: " + myString2);
    Response.Write("<br/>");
}

Open in new window

Avatar of shragi

ASKER

@wdosanjos

that worked thank you...

can you explain me the patterns... i am really not gud at regular expressions...
Avatar of shragi

ASKER

thank you  @yv989c  

and thank you  all....

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shragi

ASKER



@wdosanjos

thats awesome... a small extension... how can i get the level number from the string...

Example:

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
      Dim p1 As IASLib.IAS_RECORD_1
       Dim p As IAS_RECORD_0

the level number is = 7
IASLib.IAS_RECORD_7  the value after IASLib.IAS_RECORD_ is level number

not 1 although it has string IASLib.IAS_RECORD_1

so i should use the first identified string...

I wrote below code

if (((indexlevel = moduleCode.LastIndexOf("IASLib.IAS_RECORD")) >= -1) && levelIdentifier ==0)
                {
                    levelIdentifier = 1;
                }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial