Solved

comparing string in C#

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to compare two table schema using c#? 6 32
C# SQL BULK INSERT CLASS 5 36
String manipulation 15 49
getting id from database 5 24
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now