0

I'm trying to check if any string entered after a certain message I do have a working version in SQL that I want to use almost the same one in C#

Usage in SQL:

        ELSE IF @Msg LIKE '/Message %'

Usage in C#:

        else if (Msg == "/Message ")

I want the tool to check if any string entered right after /Message.

Thanks in advance :)

Crepitus
  • 39
  • 1
  • 6
  • 1
    Possible duplicate of [C# Version Of SQL LIKE](https://stackoverflow.com/questions/5417070/c-sharp-version-of-sql-like) – Lance U. Matthews Jun 17 '17 at 00:02
  • I don't want to search for a string in a string. I want to read any possible string entered – Crepitus Jun 17 '17 at 00:08
  • Are you already familiar with `string.StartsWith()`, `string.Contains()` and `string.EndsWith()`? – Rufus L Jun 17 '17 at 00:13
  • I'm not understanding how the two questions differ. You want to test if `Msg` contains `"/Message "` followed by arbitrary text, and optionally retrieve and act on that arbitrary text? The linked question, as well as [the many other duplicates or near-duplicates](https://www.google.com/search?q=site%3Astackoverflow.com+c%23+like+operator), provide the building blocks for how to do that. It might be better to look at this through the lens of regular expressions, not SQL, as that is what the solution will likely involve (if not the basic `string` methods @RufusL notes). – Lance U. Matthews Jun 17 '17 at 00:17
  • Which specific LIKE features do you need? See https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql . I assume you need % but do you need _ support? [] support? [^] support? etc etc – mjwills Jun 17 '17 at 01:53

3 Answers3

3

You can use the following string functions:

var Msg = "A message to check";

For LIKE 'SomeText%':

if (Msg.StartsWith("SomeText"))

For LIKE '%SomeText':

if (Msg.EndsWith("SomeText"))

For LIKE '%SomeText%':

if (Msg.Contains("SomeText"))
Rufus L
  • 36,127
  • 5
  • 30
  • 43
1

Here is how the TSQL LIKE keyword works.


using System;

namespace SqlLike
{
    class Program
    {
        static void Main(string[] args)
        {
            var findMe = "ABCHelloXYZ";

            // LIKE '......%'
            if (findMe.StartsWith("ABC"))
            {
                Console.WriteLine("Yay");
            }

            // LIKE '%......'
            if (findMe.EndsWith("XYZ"))
            {
                Console.WriteLine("Woohoo");
            }

            // LIKE '%......%'
            if (findMe.Contains("Hello"))
            {
                Console.WriteLine("JackPot!!");
            }

            Console.ReadLine();
        }
    }
}
bic
  • 2,201
  • 26
  • 27
0

Your requirements are a little vague, but this should get you there (with a little adjustment as needed).

String Msg = "/Message somethingelse";
        if (Msg.Contains("/Message ") && (Msg.Length > 9) )
        {
            String str = Msg.Substring(9);
            Console.Write("str: " + str);
        }

As @Scott suggested you can also use:

if (Msg.StartsWith("/Message ") && (Msg.Length > 9))
        {
            String str = Msg.Substring(9);
            Console.Write("str: " + str);
        }
DaniDev
  • 2,471
  • 2
  • 22
  • 29