1

I must to use .NET 2.0 and I have no Linq support here, also...

I've tried to trim chars using Remove() and Trim() functions like:

  string guid = Guid.NewGuid().ToString();
  string result = header.Trim( new Char[] { ' ', '-'} ));

But for the Trim() sample, the result is the same as string guid ( they are just equal ).

I've done such function, but I want to use correctly functions from the sealed String class.

        string guid = Guid.NewGuid().ToString(); 
        string result = String.Empty;

        for (int i = 0; i < guid.Length; i++)
        {
            if (guid[i] != '-') result += guid[i];
        }

But it looks like plain C style and I want to use exactly String function. Where did I fail with Trim() and Remove().

Don't forget, that I can't use Linq or higher version of .net 2.0, so the question is on the lower level...

Secret
  • 2,627
  • 7
  • 32
  • 46

2 Answers2

3
string guid = Guid.NewGuid().ToString("N"); //generate it the right way

or

result = guid.Replace("-", "").Replace(" ", "");

Don't use a Regex. It is unnecessarily complex and unnecessarily slow. It is not the best solution.

usr
  • 168,620
  • 35
  • 240
  • 369
1

you could always use regex

 system.text.regularexpressions.regex.replace(guid.NewGuid(),"[\s|-]","");
rerun
  • 25,014
  • 6
  • 48
  • 78
  • thanks a lot! but I'm still confused why does not Trim() work? – Secret Jan 11 '13 at 22:09
  • @mikeTheLiar in what place did you see that I'm asking about strongly and exactly about 1 thing? try to read again or I may send you some glasses via real mail not web one – Secret Jan 11 '13 at 22:44
  • @mikeTheLiar my apologies for being evil and that have argued at you... I have chat in C++ room there were only ridicule about my problem, seems to be that I'm used to the normal communication because innormal chat speeches , sorry again – Secret Jan 11 '13 at 23:02
  • @OlegOrlov no problem, it's often difficult to understand a person's true intentions over the internet. My comment was directed at the answerer, not you. – Mike G Jan 12 '13 at 22:54