Compare strings

A

Andyoye

I have two text boxes (values can in any order and in any case(upper,
lower....)

textBox1 = john, Tom, Linda, frank
textBox2 = Tom, John, Frank

How can I set textBox3 value to "Linda"? (Linda is not part of value for
both textBox1/textBox2)
 
J

Jeff Johnson

I have two text boxes (values can in any order and in any case(upper,
lower....)

textBox1 = john, Tom, Linda, frank
textBox2 = Tom, John, Frank

How can I set textBox3 value to "Linda"? (Linda is not part of value for
both textBox1/textBox2)

Use string.Split() to get the individual names. Then compare the contents of
the first array to the second using string.Compare() (for the
case-insensitivity) and look for non-matches, storing them in a
List<string>. Then compare the contents of the second array to the first,
because the missing name could be in either array. The list will now contain
all the mismatches, and you can fill the text box with those names.

I'm not giving you more than a basic blueprint because this sounds like a
homework question and I'm not going to write your code for you.
 
A

Andyoye

Jeff: thanks for the reply. This is not a homework Q


I have a form and a control is connected with outlook GAL to search users. A
workflow is attached with the form and will notify users who is pulled into
the control. Problem is, If John is already pulled in, and now I add
"frank", I dont want "john" to get email notification as well (it should
only go to "frank")

thanks
 
P

Peter Duniho

Andyoye said:
Jeff: thanks for the reply. This is not a homework Q


I have a form and a control is connected with outlook GAL to search users. A
workflow is attached with the form and will notify users who is pulled into
the control. Problem is, If John is already pulled in, and now I add
"frank", I dont want "john" to get email notification as well (it should
only go to "frank")

It seems odd to me to use the control itself as the data source. But in
any case, if you follow Jeff's suggestion, a good way to determine the
differences between two enumerations (e.g. arrays of strings) is to use
the Enumerable.Except<TSource>() extension method:

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

Pete
 
P

Paul

Stop thinking about strings and start thinking about lists of People, couple
that with a composite pattern and voila.
 
A

Arne Vajhøj

Andyoye said:
I have two text boxes (values can in any order and in any case(upper,
lower....)

textBox1 = john, Tom, Linda, frank
textBox2 = Tom, John, Frank

How can I set textBox3 value to "Linda"? (Linda is not part of value for
both textBox1/textBox2)

Example:

using System;
using System.Linq;

namespace E
{
public class Program
{
public static string StringListDiff(string s1, string s2)
{
return string.Join(", ", s1.Split(", ".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries).Except(s2.Split(",
".ToCharArray(), StringSplitOptions.RemoveEmptyEntries),
StringComparer.InvariantCultureIgnoreCase).ToArray());
}
public static void Main(string[] args)
{
string s1 = "john, Tom, Linda, frank, foo, Bar";
string s2 = "Tom, John, Frank, bar";
string s3 = StringListDiff(s1, s2);
Console.WriteLine(s3);
Console.ReadKey();
}
}
}

Arne
 

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