Hi Manuel,
Thank you for using Microsoft Newsgroup Support Service. My name is
Lingzhi Sun [MSFT]. It's my pleasure to work with you on this case.
Also thanks to Ben, Hans and Arne for your great ideas.
Although LikeOperator is under the namespace of
Microsoft.VisualBasic.CompilerServices, we can add a reference to the
Microsoft.VisualBasic assembly into our C# project, so that all the public
classes and methods inside the assembly can be used. To add the
Microsoft.VisualBasic assembly as reference, we can follow these steps:
Project References --> Add Reference… --> Under ".NET" tab, select
"Microsoft.VisualBasic" --> Press "OK". Then LikeOperator.LikeString
method can be used in C# codes as Arne's post suggested.
If you don't want to add the assembly Microsoft.VisualBasic.dll into your
C# project, you can consider realizing your own method based on the source
codes of LikeOperator.LikeString method. Reflector is one of the best
tools to see the implementation of the Microsoft.VisualBasic assembly.
Please open the file Microsoft.VisualBasic.dll in Reflector. The file is
under the folder of "%SystemRoot%\Micorosft .NET\Framework\v2.0.50727\".
This method is more difficult to realize than directly calling the method
LikeOperator.LikeString, because the implementation of this method refers
to many other classes and methods in Microsoft.VisualBasic assembly.
Another approach is to convert the pattern of LIKE operator to the pattern
of regular expression. You can refer to Hans' post. Also I provide you
with a code snippet for reference, please modify these codes to fulfill
your own target.
=======================================
private static bool vbLikeRegax(string str, string pattern)
{
pattern = pattern.Replace("!", "^")
.Replace("?", ".").Replace(@"\", @"\\")
.Replace("#", @"\d");
Regex reg = new Regex(pattern);
foreach (Match m in reg.Matches(str))
{
if (m.Success)
if (m.Length == str.Length)
return true;
}
return false;
}
=======================================
Important to note: Generally, regular expression is worse in performance
than the method LikeOperator.LikeString. The source code of
LikeOperator.LikeString tells us that it uses its own rule to parse the
string instead of using .NET Regex class. This rule is much simpler than
the rule of regular expression when parsing the pattern string. More
detailed information on LikeOperator can be found in this link:
http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx. For the additional
information on regular expression in .NET, please see
http://msdn.microsoft.com/en-us/library/hs600312.aspx.
Here is another article which discusses the performance of regular
expression and the LikeOperator.LikeString method:
http://www.dotnet2themax.com/blogs/fbalena/PermaLink,guid,53da27bf-b980-4abc
-a4e8-a452dc6ceb26.aspx.
If you have any other questions, please be free to let me know. Have a
great day!
Regards,
Lingzhi Sun (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.