Solved

comparing string in C#

Posted on 2016-11-10
3
80 Views
Last Modified: 2016-11-16
I try to find if a string 'anObject' is contain in my list of string 'objectsList', there is a double code in from of 'anObject'.  How can I remove it so I can compare properly. I try Replace a char but does not seems working. Any suggestion will be appreciated. Thanks.


Screen capture
0
Comment
Question by:tommym121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 250 total points
ID: 41882408
Since you are using the character version of the replace method, you only need to specify the " without the escape character.

Proof of concept:
using System;
using System.Collections.Generic;

namespace EE_Q28982297
{
	class Program
	{
		static void Main(string[] args)
		{
			string anObject = "\"AI 3";
			List<string> objectsList = new List<string> { "RICH_C007_TEC", "OCC_UNOCC", "VAVLE COMD", "AI 3", "CLG AOV 3", "DI 2", "DI 6", "AUX TMP AI 5" };

			if (objectsList.Contains(anObject.Replace('"', ' ').Trim()))
				Console.WriteLine("Found: {0}; at index {1}", anObject.Replace('"', ' ').Trim(), objectsList.IndexOf(anObject.Replace('"', ' ').Trim()));
			else
				Console.WriteLine("Could not find: {0}; in the object list", anObject.Replace('"', ' ').Trim());
			Console.ReadLine();
		}
	}
}

Open in new window

If you were using the string version of the replace method, then you would use the escape character; e.g. -
using System;
using System.Collections.Generic;

namespace EE_Q28982297
{
	class Program
	{
		static void Main(string[] args)
		{
			string anObject = "\"AI 3";
			List<string> objectsList = new List<string> { "RICH_C007_TEC", "OCC_UNOCC", "VAVLE COMD", "AI 3", "CLG AOV 3", "DI 2", "DI 6", "AUX TMP AI 5" };

			if (objectsList.Contains(anObject.Replace("\"", "")))
				Console.WriteLine("Found: {0}; at index {1}", anObject.Replace("\"", ""), objectsList.IndexOf(anObject.Replace("\"", "")));
			else
				Console.WriteLine("Could not find: {0}; in the object list", anObject.Replace("\"", ""));
			Console.ReadLine();
		}
	}
}

Open in new window

Both of the above produce the following output -Capture.JPG
-saige-
0
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 250 total points
ID: 41885800
You must assign the result value to anObject:

anObject = anObject.Replace('\"', ' ');
anObject  = anObject.Trim()
0
 

Author Closing Comment

by:tommym121
ID: 41890072
Thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question