Solved

comparing string in C#

Posted on 2016-11-10
3
54 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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