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



shragiAsked:
Who is Participating?
 
wdosanjosCommented:
Please try the following.  It uses regular expressions:

var strings = new string[]{
		"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)",
		"Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7())",
		"Private Sub Scan(ByVal pRoot As IASLib.IAS_RECORD_7())"};

var pattern1 = new Regex(@"(?<=\s)\w+?(?=(_[^_]+$)|$)");
var pattern2 = new Regex(@"(?<=_)[^_]+$");

foreach (var s in strings)
{
	var str = s.Substring(0, s.IndexOf('(')).Trim();

	string string1 = pattern1.Match(str).Value;
	string string2 = pattern2.Match(str).Value;

	Console.WriteLine("{0}\nstring1 = {1}\nstring2 = {2}\n", s, string1, string2);
}

Open in new window


Output:

Private Sub IE_BW_CLEANUP_Error(ByVal p As IASLib.IAS_RECORD_0, ByVal Code As Integer)
string1 = IE_BW_CLEANUP
string2 = Error

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)
string1 = Scan
string2 = Finish

Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7())
string1 = Scan
string2 = Finish

Private Sub Scan(ByVal pRoot As IASLib.IAS_RECORD_7())
string1 = Scan
string2 = 

Open in new window

0
 
radcaesarCommented:
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
0
 
shragiAuthor Commented:
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"
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Todd GerbertIT ConsultantCommented:
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

0
 
radcaesarCommented:
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.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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

0
 
shragiAuthor Commented:
@wdosanjos

that worked thank you...

can you explain me the patterns... i am really not gud at regular expressions...
0
 
shragiAuthor Commented:
thank you  @yv989c  

and thank you  all....

0
 
wdosanjosCommented:
var pattern1 = new Regex(@"(?<=\s)\w+?(?=(_[^_]+$)|$)");

(?<=\s):  Look back expression meaning that there must be a space before the match.
(?<=) look back; \s space char

\w+?: Smallest word match possible.
\w word char; + one or more; ? smallest match

(?=(_[^_]+$)|$)"): Look forward expression meaning that the match is followed by an _word or end of line.
(?=) look forward; _ char; [^_]+ one or more characters different than _; $ end of line; |$ or end of line

var pattern2 = new Regex(@"(?<=_)[^_]+$");

(?<=_): Look back expression indicating the word is prefixed with _

[^_]+: Matches one or more characters different than _

$: Anchor meaning end of line

More details about Regular Expression Language Elements on the following link:
http://msdn.microsoft.com/en-us/library/az24scfc.aspx
0
 
shragiAuthor Commented:


@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;
                }
0
 
wdosanjosCommented:
You can use the following pattern.  It gets the number after the first IASLib.IAS_RECORD_, which may or may not be on the method signature (in case it does not have it).

var pattern3 = new Regex(@"(?<=IASLib\.IAS_RECORD_)\d+", RegexOptions.IgnoreCase);
var s = "Private Sub Scan_Finish(ByVal pRoot As IASLib.IAS_RECORD_7)\n	  Dim p1 As IASLib.IAS_RECORD_1\n	   Dim p As IAS_RECORD_0";

var levelIdentifier = pattern3.Match(s).Value;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.