Solved

comparing string in C#

Posted on 2016-11-10
3
68 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
3 Comments
 
LVL 33

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
.NET 2008 VB and C# 6 47
C# Service FileSystemwatcher isse 9 46
Converting PHP triple des decrypt to c# 9 62
c#, case, if 4 17
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

761 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