Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

C #: Check for Contains string without a large scale

 

How do you check a C # string for a substring? The capitalization and lower case should not play a role here.

 

Solution:

With IndexOf and the text comparison * IgnoreCase it is checked, whether a partial text occurs in a text.

Sting.IndexOf("_all", StringComparison.InvariantCultureIgnoreCase) >-1

 

Standard without ignoring big / small

Check for occurrences of a substring with uppercase / lowercase

If you do not set a StringComparison parameter, the default is case-sensitive.

bool IsOwn = false;

if (sAction.IndexOf("_own) >0) { IsOwn = true; }

 

If there is no match, the result is -1

correction

if (sAction.IndexOf("_all", StringComparison.InvariantCultureIgnoreCase) >-1 || sAction.ToLower() =="index")

{

    <a asp-controller="Notes" asp-action="Index_All" class="mynav_a_bg50" style="@sStyle_Activ_Notes">Notizen</a>

}

else

{

    <a asp-controller="Notes" asp-action="Index_Own" class="mynav_a_bg50" style="@sStyle_Activ_Notes">Notizen</a>

}

 

Intellisense: IndexOf returns the parameters

StringComparison.CurrentCulture

StringComparison.CurrentCultureIgnoreCase

StringComparison.InvariantCulture

StringComparison.InvariantCultureIgnoreCase

StringComparison.Ordinal

StringComparison.OrdinalIgnoreCase

 

 

In the debugger you can see that when comparing with * IgnoreCase the match occurs with character 5, but with the standard comparison with .IndexOf (..) nothing is found and the result is -1

 

 

Substring, Substring, Instr, InstRev, Contains

Mobile

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de