Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case sensitive string compariason

Status
Not open for further replies.

jbenson001

Programmer
Jan 7, 2004
8,172
US
Hi all,

I am devloping a vb app in VS2005. I noticed that the string comparison is case sensitive. Is there a global(project or solution) setting that can be changed so that string comparisons are case-insensitive?

Thanks

Jim
 
depending on the string you are comparing, you could just create a couple temp variables and Upper() them, then do your comparison.

SWM Programmer Seeking 3d Modeling work. Either for long term commitment or just friendship. Likes long hours in front of a flat screen with heavy doses of Mountain Dew.
 
Yeah, I figured that work around. I just find it strange that you can't, as far as I can find, change it globally, like you can with SQL Server.
 
you could just write your own string comparision function. One may already exist, and you just have to find it.

SWM Programmer Seeking 3d Modeling work. Either for long term commitment or just friendship. Likes long hours in front of a flat screen with heavy doses of Mountain Dew.
 
Yeah I know that is an option, I just figured that something like string comparisons and case sensitivity would be able to be set globally. If not, I will just convert to lower or upper and compare.

Thanks
 
create a string and check out the .Equals function uses parameter set 2, I think that's what you are looking for.

Code:
strTemp.Equals(strTemp2,StringComparison.OrdinalIgnoreCase)

Don't worry about the problem.
Think about the solution, then it's not a problem.
 
Staright from our feiznd MSDN

Declares the default comparison method to use when comparing string data.


Option Compare { Binary | Text }


Parts
Binary
Optional. Results in string comparisons based on a sort order derived from the internal binary representations of the characters.

Text
Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.

Remarks
If used, the Option Compare statement must appear in a file before any other source code statements.

The Option Compare statement specifies the string comparison method (Binary or Text) for a class, module or structure. If an Option Compare statement is not included, the default text comparison method is Binary.

In Microsoft Windows, sort order is determined by the code page. For more information, see Code Pages.

In the following example, characters in the English/European code page (ANSI 1252) are sorted using Option Compare Binary, which produces a typical binary sort order.

A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø

When the same characters in the same code page are sorted using Option Compare Text, the following text sort order is produced.

(A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø)

You can also set Option Compare in the Visual Studio integrated development environment (IDE) or on a command line.

To set Option Compare in the integrated development environment (IDE)
On the Tools menu, choose Options.

Open the Projects and Solutions node.

Choose VB Defaults.

Modify the Option Compare setting.

To set Option Compare on the command line
Include the /optioncompare compiler option in the vbc command.

Example
The following example uses the Option Compare statement to set the binary comparison as the default string comparison method.

Visual Basic Copy Code
' Set the string comparison method to Binary ("AAA" < "aaa").
Option Compare Binary



The following example uses the Option Compare statement to set the case-insensitive text sort order as the default string comparison method.

Visual Basic Copy Code
' Set the string comparison method to Text ("AAA" = "aaa").
Option Compare Text

Christiaan Baes
Belgium

"My new site" - Me
 
I tend to use this method:

Code:
Option Compare Text

Public Class CaseInsensitiveStuff

	Public Shared Function CompareString(ByVal s1 As String, ByVal s2 As String) As Boolean

		Return s1 = s2

	End Function

End Class

Code:
Option Compare Binary

Public Class CaseSensitiveStuff

	Public Shared Function CompareString(ByVal s1 As String, ByVal s2 As String) As Boolean

		Return s1 = s2

	End Function

End Class


and to test them:

Code:
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		If CaseSensitiveStuff.CompareString(TextBox2.Text, TextBox3.Text) Then
			MessageBox.Show("Identical")
		End If
		If CaseInsensitiveStuff.CompareString(TextBox2.Text, TextBox3.Text) Then
			MessageBox.Show("Textually identical")
		End If

	End Sub


The Option Compare Binary / Text statement at the top of a file determines the case sensitivity of text comparisons within that file.


Hope this helps.



[vampire][bat]
 
Looks german to me. I wonder wich one of my personalities wrote that. It certainly wasn't this one.

Christiaan Baes
Belgium

"My new site" - Me
 
Thank you all very much for your help. Christiaan, I did see that option before but must have thought it was not relavant. I did however try it and set it to text, however, it still did not work.
I think i will just stick with the quick and dirty way of using .ToLower or .ToUpper and comparing.

Again.. thanks everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top