shragi
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
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
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"
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"
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);
}
}
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.
If it has only one _ get second, if it have two _ then get third like that.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/>");
}
ASKER
@wdosanjos
that worked thank you...
can you explain me the patterns... i am really not gud at regular expressions...
that worked thank you...
can you explain me the patterns... i am really not gud at regular expressions...
ASKER
thank you @yv989c
and thank you all....
and thank you all....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("IA
{
levelIdentifier = 1;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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