3

I have a requirement wherein I have a value in Viewbag.Keyword like "Johnson and Jen" so I have to split this value and compare it with List.

I was trying the below code but facing error some or the other way(eg; this is char not string and so on) and it doesn't look like smart coding as per latest functionalities available in framework 4.5

var str = Convert.ToString(ViewBag.Keyword);
string[] str1 = str.Split(' ');

for (int j = 0; j < str1.Length;j++)
{
    string str2 = Convert.ToString(str[j]);
    if (result.Document.ContactName.IndexOf(str2, 
        StringComparison.CurrentCultureIgnoreCase) != -1)

Note: "ContactName" is string. Please help with some latest code which improves performance.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
harshu288
  • 141
  • 1
  • 12

5 Answers5

1

You can use linq to check if result.Document.Name contains a word from array.

var str = Convert.ToString(ViewBag.Keyword);
string[] str1 = str.Split(' ');
var names = str1.Where(x => (result.Document.ContactName.Contains(x))).ToList();
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
1

You can compare those names are on that string like below,

Remember comparison is case sensitive

var str = Convert.ToString(ViewBag.Keyword);
string[] str1 = str.Split(' ');

if(str1.Any(result.Document.ContactName.Contains)){
    //Do Something
}

Example -

var str = "Johnson and Jen";
string[] str1 = str.Split(' ');
string result = "Jendasdbnoahsdasnd"; // I assume your ContactName result is like this
if (str1.Any(result.Contains)) // This gives true because Jen in the string
{
  //Do Something
}

But, for the string result = "jendasdbnoahsdasnd" it gives false because of the case.

SilentCoder
  • 1,970
  • 1
  • 16
  • 21
0

Below is a sample code for doing the same using clean code

var str = Convert.ToString(ViewBag.Keyword);
var possibleNames = str.split(' ');

//to check if any match exists
bool anyMatch = possibleNames.Any(n => n.Equals(result.Document.ContactName, StringComparison.InvariantCultureIgnoreCase));

//get matched names
var matchedNames = possibleNames.Where(n => n.Equals(result.Document.ContactName, StringComparison.InvariantCultureIgnoreCase));

//do whatever you want with matchedNames now
var array = matchedNames.ToArray();
var list = matchedNames.ToList();
Sarvesh Mishra
  • 2,014
  • 15
  • 30
0

You can split the string "Johnson and Jen" by using another string i.e. " and " this will exactly give you the names which will be appropriate to check against the contact name.

You can check about the Split function here and here

To check whether a property contains a specified string or not you can use the IndexOf method - a simple sample code snippet is given below, you can run this sample code snippet here.

var keyword = "Johnson and Jen";
var arr = keyword.Split(new string[] { " and " }, StringSplitOptions.None);
var contactName = "Johnson";
for (var i = 0; i < arr.Length; i++)
{
    if (contactName.IndexOf(arr[i], StringComparison.OrdinalIgnoreCase) >= 0)
    {
        // do something
        Console.WriteLine("contactName contains the string \"" + arr[i] + "\"");
    }
}

The code snippet for your scenario would be as follows and in your code you don't need the unnecessary type casting i.e. string str2 = Convert.ToString(str[j]); because str[j] is already a string

var keyword = Convert.ToString(ViewBag.Keyword);
var arr = name.Split(new string[] { " and " }, StringSplitOptions.None);

for (int i = 0; i < arr.Length; i++)
{
    if (result.Document.ContactName.IndexOf(arr[i], StringComparison.OrdinalIgnoreCase) != -1)
    {
        // do something
    }
}

Using the StringComparison parameter with its value as StringComparison.OrdinalIgnoreCase will do a case insensitive search

Community
  • 1
  • 1
Abdul Mateen Mohammed
  • 1,864
  • 1
  • 12
  • 21
  • Well I have already tried most of the scenarios, I need exact match of the word from the collection. Current issue eg: Contact Name **"Johnson anderson mobile"** Here results returned are "Johnson anderson" --> **"anderson"** because it matches **"and"**, which I don't require, please help me with the fix. – harshu288 Aug 31 '16 at 10:21
0

If you need to check for the exact match of the words you get by splitting the keyword string using a white space against the contact name property from the collection then you can use a regular expression to do that.

The regular expression is of the form \bTheWordGoesHere\b where \b is the word boundary delimiter

Check this sample code snippet.

var keyword = "Johnson and Jen";
var arr = keyword.Split(' ');
var contactName = "Johnson anderson mobile";
for (var i = 0; i < arr.Length; i++)
{
    var pattern = string.Format("\\b{0}\\b", arr[i]);
    if (Regex.IsMatch(contactName, pattern, RegexOptions.IgnoreCase))
    {
        // do something
        Console.WriteLine("contactName contains the word \"" + arr[i] + "\"");
    }
}

rextester link

Abdul Mateen Mohammed
  • 1,864
  • 1
  • 12
  • 21