Really annoying string problem

string literals and strings

Page 1 of 1

2 Replies - 1953 Views - Last Post: 14 January 2008 - 05:09 AM Rate Topic: -----

#1 weedweaver  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 42
  • Joined: 01-May 07

Really annoying string problem

Posted 14 January 2008 - 03:19 AM

Im having a really annoying string problem. I have used the Registry.LocalMachine.Opensubkey method and then got the value of a particular key as a string. this comes back as a string literal e.g.

string test = reg.Getvalue("myregistryValue")

looking at this in the debugger it comes back as @"Fred" (for example)

I have decalred another string called test2 as:

test2 = "Fred"

Believe it or not (test == test2) fails, Strring.compareto and String.compare also fails. these strings are exactly the same apart from test is a string literal.

Any ideas how to compare these two??

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Really annoying string problem

#2 weedweaver  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 42
  • Joined: 01-May 07

Re: Really annoying string problem

Posted 14 January 2008 - 03:51 AM

the strings contain backslashes and if i do a length on both of them they come back with different lengths. the comparison works if they are both string literals but how do i convert from a string to a string literal, simply putting an @ sign in front of it doesnt work, even in the declaration (if its the result of a method) e.g.

string test2 = @Encryption.Encrypt(string) etc.. (for example)

Any ideas?

This post has been edited by weedweaver: 14 January 2008 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Really annoying string problem

Posted 14 January 2008 - 05:09 AM

View Postweedweaver, on 14 Jan, 2008 - 05:51 AM, said:

the strings contain backslashes and if i do a length on both of them they come back with different lengths.


If they're not the same length, they're not the same string. C# is pretty good about String.Equals. Try this code:

using System;
using System.Diagnostics;

namespace TestEqual {
	class Program {
		static string GetStrInfo(string s) {
			String retVal = "s=" + s + ";len=" + s.Length;
			foreach (char c in s.ToCharArray()) { 
				retVal += "(" + c + ":" + (int)c + ")";
			}
			return retVal;
		}

		static void Main(string[] args) {
			Debug.WriteLine(GetStrInfo("foo"));
			Debug.WriteLine(GetStrInfo("foo" + (char)1));
		}
	}
}



Do a dump on both strings and see what you're missing.

What's the goal? If you're trying to compare, you could sanitize your result to be only letters and numbers.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1