Click here to Skip to main content
15,923,120 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am looking for a best method to extract the words.

I have sentence called TestTest - CustID : 1200005#14102016_0412- ARF or it may be TestTest - CustID:1200005#14102016_0412- ARF

This 'CustID : 1200005#' may be with or without spaces. I need to extract '1200005' from this.

C#
string subject = "TestTest - CustID : 1200005#14102016_0412- ARF"

if (subject.Trim().Contains(CustID:))
                {
                    customerId = ExtractFromString(mailSubject, "CustID:","#");
                }
                else if (subject.Trim().Contains(CustID:))
                {
                    customerId = ExtractFromString(mailSubject, "CustID:","#");
                }
}
 
<pre lang="C#">private static List&lt;string&gt; ExtractFromString(string text, string startString, string endString)
       {
           List&lt;string&gt; matched = new List&lt;string&gt;();
           int indexStart = 0, indexEnd = 0;
           bool exit = false;
           while (!exit)
           {
               indexStart = text.IndexOf(startString);
               indexEnd = text.IndexOf(endString);
               if (indexStart != -1 &amp;&amp; indexEnd != -1)
               {
                   matched.Add(text.Substring(indexStart + startString.Length,
                       indexEnd - indexStart - startString.Length));
                   text = text.Substring(indexEnd + endString.Length);
               }
               else
                   exit = true;
           }
           return matched;
       }</pre>



This is working but i am looking for some better option considering both CustID : 1200005# or CustID:1200005#

What I have tried:

What i tried has been appended.


C#
string subject = "TestTest - CustID : 1200005#14102016_0412- ARF"
if (subject.Trim().Contains("CustID"))
                {
                    customerId = ExtractFromString(mailSubject, "CustID:","#");
                }
                else if (subject.Trim().Contains(CustID:))
                {
                    customerId = ExtractFromString(mailSubject, "CustID:","#");
                }
}

<pre lang="C#">private static List&lt;string&gt; ExtractFromString(string text, string startString, string endString)
       {
           List&lt;string&gt; matched = new List&lt;string&gt;();
           int indexStart = 0, indexEnd = 0;
           bool exit = false;
           while (!exit)
           {
               indexStart = text.IndexOf(startString);
               indexEnd = text.IndexOf(endString);
               if (indexStart != -1 &amp;&amp; indexEnd != -1)
               {
                   matched.Add(text.Substring(indexStart + startString.Length,
                       indexEnd - indexStart - startString.Length));
                   text = text.Substring(indexEnd + endString.Length);
               }
               else
                   exit = true;
           }
           return matched;
       }</pre>
Posted
Updated 16-Oct-16 7:58am
v2

You're over-engineering it.

C#
string text = "TestTest - CustID : 1200005#14102016_0412- ARF";
string[] parts = text.Split(':');
string[] parts2 = parts[1].Split('#');
string result = parts2[0].Trim();


or even

C#
string text = "TestTest - CustID : 1200005#14102016_0412- ARF";
string result = text.Split(':')[1].Split('#')[0].Trim();


If you need some error trapping in there, you should probably use the 1st version.
 
Share this answer
 
v3
Comments
W Balboos, GHB 14-Oct-16 8:39am    
Very Nice (+)
Maciej Los 16-Oct-16 5:10am    
5ed!
Quote:
This is working

Are you sure ?
C#
string subject = "TestTest - CustID : 1200005#14102016_0412- ARF"
if (subject.Trim().Contains("CustID"))
{
    if (subject.Trim().Contains(CustomerID:))
    {
        customerId = ExtractFromString(mailSubject, "CustomerID:","#");
    }
    else if (subject.Trim().Contains(CustID:))
    {
        customerId = ExtractFromString(mailSubject, "CustomerID:","#");
    }
}

I see 2 syntax errors, 3 misuse and at 4 error of logic.
And from the code, I suspect the example to not be complete.
I expect 2 main examples with optional spaces.

it stings my eyes.

So what is the question and the problem ?

[UpDate]
When I see your new code:
C#
if (subject.Trim().Contains("CustID"))
    {
        customerId = ExtractFromString(mailSubject, "CustID:","#");
    }
    else if (subject.Trim().Contains(CustID:))
    {
        customerId = ExtractFromString(mailSubject, "CustID:","#");
    }
}

You do not understand what this code does, it can't be yours, it do not even compile !
Advice: Learn properly C#
 
Share this answer
 
v4
Comments
#realJSOP 14-Oct-16 12:35pm    
He was looking for something that would extract the actual customer ID regardless of the inclusion/location of spaces in the string being parsed. See my solution.
Patrice T 14-Oct-16 12:45pm    
I know, but he also claim that the code is working ! :)
Solution 1 by John Simmons / outlaw programmer[^] is very good. As an alternative you can use Regex class[^] which is used to find matched string.

I'd suggest to use Regex.Match[^] method:

C#
string subject = "TestTest - CustID : 1200005#14102016_0412- ARF";
string pattern = @"\d{1,}#";

Match m = Regex.Match(subject, pattern, RegexOptions.Singleline);
if(m.Success)
	Console.WriteLine(m.Value.Replace("#",""));


Note: change the pattern to your needs.

For further details, please, see:
Regular Expression Language - Quick Reference[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900