Converting MSSQL FTS parsing function from VBScript to C#

G

Guest

Hi all,

I'm here to humbly request some "gimme" help from the group. I discovered
the infinite wonders of VS.NET 2005 and ASP.NET 2.0 at Tech Ed a couple of
weeks ago, and since returning to work I haven't looked back. Unfortunately
I still have some critical functions that I originally created as vbscript
functions. I need these functions to do my work. Before I can transition
entirely to C#-based ASP.NET sites, I need these functions to be translated
to C# syntax.

I've managed most on my own. The most difficult to translate is a function I
made to prepare a text string so that it is in the proper format demanded by
SQL Server's awesome Full Text Search capability.

Ideally, someone knows of an analogous function that's already been hacked
up in C#. I'd imagine this particular wheel has been reinvented a few
billion times.

Barring that, I'm hoping some of the sharper Csharpers will hold my hand and
help me translate the function to C#. It's straight string manipulation
stuff, and appears below. Thanks in advance for any help you can offer.

Ken Fine
University of Washington



Function CreateSQLContainsString(s, requireAllWords)
dim i, start, result, sLen, conjunction, token, fSeparator
result = ""
start = 1
sLen = Len(s)
fSeparator = False

If requireAllWords Then
conjunction = " AND"
Else
conjunction = " OR"
End If

For i = 1 to sLen
ch = Mid(s, i, 1)

If (ch="""") Then
i = i + 1
While (i < sLen AND Mid(s,i,1) <> """")
i = i + 1
Wend
End If

If (ch=" " or i = sLen) Then
token = Trim(Mid(s, start, i-start+1))
utoken = UCase(token)
If start = 1 Then
result = token
ElseIf (utoken = "AND" or utoken="OR" or utoken="NEAR") Then
result = result + " " + utoken
fSeparator = TRUE
Else
If fSeparator = FALSE Then
result = result + conjunction
End If
result = result + " " + token
fSeparator = FALSE
End If
start = i + 1
End If
Next
CreateSQLContainsString = result
End Function
 
D

Dan Bass

just a straight port really...



string CreateSqlContainsString ( string s, bool requireAllWords )
{
string conjunction = requireAllWords ? " AND " : " OR " ;
string ch;
string token;
System.Text.StringBuilder result = new
System.Text.StringBuilder();
bool bSeperator = false;

int start =1;

for ( int i = 0; i < s.Length-1; i++ )
{
ch = s.Substring ( i, 1 );

if ( ch == "\"" )
{
i++;
while ( i < s.Length && s.Substring(i,1) != "\"" )
{
i++;
}
}

if ( ch == " " )
{
token = s.Substring ( start, i-start+1 );
string uToken = token.ToUpper();
if ( start == 1 )
{
result = new System.Text.StringBuilder();
result.Append(token);
}
else if ( uToken == "AND" || uToken == "OR" || uToken ==
"NEAR" )
{
result.Append ( " " + uToken );
bSeperator = true;
}
else
{
if ( !bSeperator )
{
result.Append ( conjunction );
}
result.Append ( " " + token );
bSeperator = false;
}
start = i+1;
}

}

return result.ToString();

}
 
G

Guest

Thank you +very+ much, Dan. I owe you one.

Let me know if you ever need graphics help or writing/editing assistance. In
a past life, I ...

-KF
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top