Solved

comparing string in C#

Posted on 2016-11-10
3
61 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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
designing in object programming 12 94
Reading the Web.Config using IIS 7.5? 4 49
SqlDataBase 7 53
C#.net split string and then check appropriate checkboxlist 4 27
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

860 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