Why is there no Char.Empty like String.Empty?












220















Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings.



Edit: The reason for this usage was this:



myString.Replace ('c', '')


So remove all instances of 'c's from myString.










share|improve this question




















  • 1





    Yeah I used that word for lack of a better word. i.e. the recommended way of using String.Empty instead of "".

    – Joan Venge
    Sep 8 '10 at 17:51











  • Thanks, do you know why it's not recommended anymore? Is it because of the compiler does it for you?

    – Joan Venge
    Sep 8 '10 at 18:02






  • 3





    If you're concerned about accidentally mistyping '' sometimes, why not just simply wrap the functionality in an extension method along the lines of RemoveAll(this string s, params char toRemove)? The intent will be clearly communicated and you will not risk mistyping anything.

    – bzlm
    Oct 5 '10 at 11:38








  • 11





    @Henk - The only reason I use string.Empty is because I find the null object provided by Empty expresses intent better than empty quotes. Empty quotes could result from a merge problem, or a bungled thought, or it could be the actual intent of that code, whereas Empty explicitly tells me that the developer intended for that string not to have data.

    – Ritch Melton
    May 21 '11 at 0:33






  • 3





    There is a difference between "" and the string.Empty. Not that anyone care, really, but "" creates an object, whereas string.Empty makes use of one already made. But again, it is so small, that only special situations it would make a diference

    – marcelo-ferraz
    Nov 30 '11 at 12:35
















220















Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings.



Edit: The reason for this usage was this:



myString.Replace ('c', '')


So remove all instances of 'c's from myString.










share|improve this question




















  • 1





    Yeah I used that word for lack of a better word. i.e. the recommended way of using String.Empty instead of "".

    – Joan Venge
    Sep 8 '10 at 17:51











  • Thanks, do you know why it's not recommended anymore? Is it because of the compiler does it for you?

    – Joan Venge
    Sep 8 '10 at 18:02






  • 3





    If you're concerned about accidentally mistyping '' sometimes, why not just simply wrap the functionality in an extension method along the lines of RemoveAll(this string s, params char toRemove)? The intent will be clearly communicated and you will not risk mistyping anything.

    – bzlm
    Oct 5 '10 at 11:38








  • 11





    @Henk - The only reason I use string.Empty is because I find the null object provided by Empty expresses intent better than empty quotes. Empty quotes could result from a merge problem, or a bungled thought, or it could be the actual intent of that code, whereas Empty explicitly tells me that the developer intended for that string not to have data.

    – Ritch Melton
    May 21 '11 at 0:33






  • 3





    There is a difference between "" and the string.Empty. Not that anyone care, really, but "" creates an object, whereas string.Empty makes use of one already made. But again, it is so small, that only special situations it would make a diference

    – marcelo-ferraz
    Nov 30 '11 at 12:35














220












220








220


17






Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings.



Edit: The reason for this usage was this:



myString.Replace ('c', '')


So remove all instances of 'c's from myString.










share|improve this question
















Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings.



Edit: The reason for this usage was this:



myString.Replace ('c', '')


So remove all instances of 'c's from myString.







c# .net string char bcl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '17 at 22:38









JohnOsborne

440620




440620










asked Sep 8 '10 at 17:44









Joan VengeJoan Venge

102k174401625




102k174401625








  • 1





    Yeah I used that word for lack of a better word. i.e. the recommended way of using String.Empty instead of "".

    – Joan Venge
    Sep 8 '10 at 17:51











  • Thanks, do you know why it's not recommended anymore? Is it because of the compiler does it for you?

    – Joan Venge
    Sep 8 '10 at 18:02






  • 3





    If you're concerned about accidentally mistyping '' sometimes, why not just simply wrap the functionality in an extension method along the lines of RemoveAll(this string s, params char toRemove)? The intent will be clearly communicated and you will not risk mistyping anything.

    – bzlm
    Oct 5 '10 at 11:38








  • 11





    @Henk - The only reason I use string.Empty is because I find the null object provided by Empty expresses intent better than empty quotes. Empty quotes could result from a merge problem, or a bungled thought, or it could be the actual intent of that code, whereas Empty explicitly tells me that the developer intended for that string not to have data.

    – Ritch Melton
    May 21 '11 at 0:33






  • 3





    There is a difference between "" and the string.Empty. Not that anyone care, really, but "" creates an object, whereas string.Empty makes use of one already made. But again, it is so small, that only special situations it would make a diference

    – marcelo-ferraz
    Nov 30 '11 at 12:35














  • 1





    Yeah I used that word for lack of a better word. i.e. the recommended way of using String.Empty instead of "".

    – Joan Venge
    Sep 8 '10 at 17:51











  • Thanks, do you know why it's not recommended anymore? Is it because of the compiler does it for you?

    – Joan Venge
    Sep 8 '10 at 18:02






  • 3





    If you're concerned about accidentally mistyping '' sometimes, why not just simply wrap the functionality in an extension method along the lines of RemoveAll(this string s, params char toRemove)? The intent will be clearly communicated and you will not risk mistyping anything.

    – bzlm
    Oct 5 '10 at 11:38








  • 11





    @Henk - The only reason I use string.Empty is because I find the null object provided by Empty expresses intent better than empty quotes. Empty quotes could result from a merge problem, or a bungled thought, or it could be the actual intent of that code, whereas Empty explicitly tells me that the developer intended for that string not to have data.

    – Ritch Melton
    May 21 '11 at 0:33






  • 3





    There is a difference between "" and the string.Empty. Not that anyone care, really, but "" creates an object, whereas string.Empty makes use of one already made. But again, it is so small, that only special situations it would make a diference

    – marcelo-ferraz
    Nov 30 '11 at 12:35








1




1





Yeah I used that word for lack of a better word. i.e. the recommended way of using String.Empty instead of "".

– Joan Venge
Sep 8 '10 at 17:51





Yeah I used that word for lack of a better word. i.e. the recommended way of using String.Empty instead of "".

– Joan Venge
Sep 8 '10 at 17:51













Thanks, do you know why it's not recommended anymore? Is it because of the compiler does it for you?

– Joan Venge
Sep 8 '10 at 18:02





Thanks, do you know why it's not recommended anymore? Is it because of the compiler does it for you?

– Joan Venge
Sep 8 '10 at 18:02




3




3





If you're concerned about accidentally mistyping '' sometimes, why not just simply wrap the functionality in an extension method along the lines of RemoveAll(this string s, params char toRemove)? The intent will be clearly communicated and you will not risk mistyping anything.

– bzlm
Oct 5 '10 at 11:38







If you're concerned about accidentally mistyping '' sometimes, why not just simply wrap the functionality in an extension method along the lines of RemoveAll(this string s, params char toRemove)? The intent will be clearly communicated and you will not risk mistyping anything.

– bzlm
Oct 5 '10 at 11:38






11




11





@Henk - The only reason I use string.Empty is because I find the null object provided by Empty expresses intent better than empty quotes. Empty quotes could result from a merge problem, or a bungled thought, or it could be the actual intent of that code, whereas Empty explicitly tells me that the developer intended for that string not to have data.

– Ritch Melton
May 21 '11 at 0:33





@Henk - The only reason I use string.Empty is because I find the null object provided by Empty expresses intent better than empty quotes. Empty quotes could result from a merge problem, or a bungled thought, or it could be the actual intent of that code, whereas Empty explicitly tells me that the developer intended for that string not to have data.

– Ritch Melton
May 21 '11 at 0:33




3




3





There is a difference between "" and the string.Empty. Not that anyone care, really, but "" creates an object, whereas string.Empty makes use of one already made. But again, it is so small, that only special situations it would make a diference

– marcelo-ferraz
Nov 30 '11 at 12:35





There is a difference between "" and the string.Empty. Not that anyone care, really, but "" creates an object, whereas string.Empty makes use of one already made. But again, it is so small, that only special situations it would make a diference

– marcelo-ferraz
Nov 30 '11 at 12:35












19 Answers
19






active

oldest

votes


















234














There's no such thing as an empty char. The closest you can get is '', the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse "" and " "" arguments don't apply for ''.



If you could give an example of where you'd want to use it and why you think it would be better, that might help...






share|improve this answer





















  • 2





    Isn't the the 'end of the byte array'-character? Or am I confusing with something else?

    – Bertvan
    Sep 8 '10 at 17:47






  • 4





    @Bertvan: Why would there be a character at the end of a byte array? It's used for "null terminated" strings though, yes.

    – Jon Skeet
    Sep 8 '10 at 17:48






  • 25





    Char.MinValue is better than ''

    – Aliostad
    Sep 8 '10 at 17:50






  • 8





    @Aliostad: Out of interest, if there was a similar field for Int32.Zero, would you use that instead of the literal 0? If not, what's the difference here?

    – Jon Skeet
    Sep 8 '10 at 18:17






  • 7





    @Adam, @Jon -- what is the code for bell? Or backspace better, think think... Or maybe instead of thinking it is just better to write Char.Backspace? Another reason -- you say it is better to write '0' for terminator, instead, say Char.Terminator, however it is not -- it is too easy to make a typo (fully compiled, see above), but try to write Char.Termnator. There are enough reasons for me to avoid non-checkable, raw values (space missions failed because of stupid typos like that).

    – greenoldman
    Sep 9 '10 at 6:19





















73














A char, unlike a string, is a discrete thing with a fixed size. A string is really a container of chars.



So, Char.Empty doesn't really make sense in that context. If you have a char, it's not empty.






share|improve this answer



















  • 2





    Exactly right. It makes sense to ask if a container is empty or not. It makes no sense to ask of a int or float or char is empty.

    – T.E.D.
    Sep 8 '10 at 18:10






  • 1





    @Joe: Then how can a string be empty if a string is a collection of (non-empty) chars? Probably stupid, sorry...

    – user1477388
    Jan 29 '13 at 16:32








  • 6





    Because a string isn't the individual objects, it's the collection. Think of a bucket of rocks. I can't have an empty rock. But I can have an empty bucket.

    – Joe
    Jan 29 '13 at 17:42






  • 1





    I would phrase it as "a char is a primitive, value type, and a string is non-primitive, reference type".

    – samis
    Aug 26 '13 at 14:26






  • 2





    This is the real answer.

    – Gandalf458
    Aug 10 '17 at 21:48



















28














There's no such thing as an empty character. It always contains something. Even '' is a character.






share|improve this answer



















  • 6





    +1 nice channeling of sir jon!

    – kenny
    Sep 8 '10 at 19:20



















22














Use Char.MinValue which works the same as ''. But be careful it is not the same as String.Empty.






share|improve this answer


























  • Thanks, haven't seen that before. Do you know if it work in myString.Replace('c', Char.MinValue)? I should give it a try.

    – Joan Venge
    Sep 8 '10 at 17:50



















19














You could use nullable chars.



char? c





share|improve this answer
























  • This allows ''? Or just null?

    – Joan Venge
    Sep 8 '10 at 17:57











  • In your case, you could do this: myString.Replace("c", (c == null ? "" : c.ToString()))

    – paquetp
    Sep 8 '10 at 18:13



















10














If you don't need the entire string, you can take advantage of the delayed execution:



public static class StringExtensions
{
public static IEnumerable<char> RemoveChar(this IEnumerable<char> originalString, char removingChar)
{
return originalString.Where(@char => @char != removingChar);
}
}


You can even combine multiple characters...



string veryLongText = "abcdefghijk...";

IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
.RemoveChar('c')
.RemoveChar('d')
.Take(5);


... and only the first 7 characters will be evaluated :)



EDIT: or, even better:



public static class StringExtensions
{
public static IEnumerable<char> RemoveChars(this IEnumerable<char> originalString,
params char removingChars)
{
return originalString.Except(removingChars);
}
}


and its usage:



        var veryLongText = "abcdefghijk...";
IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
.RemoveChars('c', 'd')
.Take(5)
.ToArray(); //to prevent multiple execution of "RemoveChars"





share|improve this answer





















  • 1





    Genius example.

    – Joan Venge
    Sep 8 '10 at 19:41











  • @Joan: thanks... even if "Genius" it's a bit exaggerated :P (I don't know about its performances when removingChars will become a big array...)

    – Notoriousxl
    Sep 8 '10 at 19:59






  • 1





    Yesterday I forgot: pay attention on how you are using the result variable "firstFiveCharsWithoutCsAndDs". If you don't want to pass it to another "yield" method (like those of LINQ), call immediately a ".ToArray()" after the "Take(5)"... otherwise, the "RemoveChars + Take" chain will be executed every time you access the variable in a "traditional" fashion (for example, every you call a "Count()" on it, or when you traverse it in a foreach without "yield return")

    – Notoriousxl
    Sep 9 '10 at 19:07











  • +1 nice thinking. but this can't get as maintainable or efficient as the basic approach :)

    – nawfal
    Feb 5 '13 at 9:51








  • 1





    @nawfal efficiency-wise you're right, but I think that myString.Except("c") is more declarative than myString.Replace('c', '') :P (and it scales pretty well: myString.Except("aeiou"))

    – Notoriousxl
    Feb 5 '13 at 18:28



















5














the same reason there isn't an int.Empty. Containers can be empty. Scalar values cannot be. If you mean 0 (which is not empty), then use ''. If you mean null, then use null :)






share|improve this answer



















  • 2





    null is not possible as char is a ValueType. You'd have to use char? to be able to assign null to it.

    – Femaref
    Sep 8 '10 at 17:53











  • you chould make it nullable. see my answer

    – paquetp
    Sep 8 '10 at 17:54











  • Good point man.

    – Joan Venge
    Sep 8 '10 at 17:56



















5














A char is a value type, so its value cannot be null. (Unless it is wrapped in a Nullable container).



Since it can't be null, in contains some numeric code and each code is mapped to some character.






share|improve this answer































    4














    myString = myString.Replace('c'.ToString(), "");


    OK, this is not particularly elegant for removing letters, since the .Replace method has an overload that takes string parameters. But this works for removing carriage returns, line feeds, tabs, etc. This example removes tab characters:



    myString = myString.Replace('t'.ToString(), "");





    share|improve this answer

































      3














      Not an answer to your question, but to denote a default char you can use just



      default(char)


      which is same as char.MinValue which in turn is same as . One shouldn't use if for something like an empty string though.






      share|improve this answer































        1














        Doesn't answer your first question - but for the specific problem you had, you can just use strings instead of chars, right?:



        myString.Replace("c", "")


        There a reason you wouldn't want to do that?






        share|improve this answer

































          1














          You can also rebuild your string character by character, excluding the characters that you want to get rid of.



          Here's an extension method to do this:



              static public string RemoveAny(this string s, string charsToRemove)
          {
          var result = "";
          foreach (var c in s)
          if (charsToRemove.Contains(c))
          continue;
          else
          result += c;

          return result;
          }


          It's not slick or fancy, but it works well.



          Use like this:



          string newString = "My_String".RemoveAny("_"); //yields "MyString"





          share|improve this answer
























          • Use a StringBuilder for result. Why not wrap return s.Replace(charsToRemove,"");?

            – aloisdg
            Jul 9 '16 at 15:26





















          1














          Easiest way to blanket remove a character from string is to Trim it



          cl = cl.Trim(' ');



          Removes all of the spaces in a string






          share|improve this answer
























          • This is helpful if one wants to use use .Replace('c', ' ') with the downside of removing other whitespaces. But its more helpful than lots of other answers given.

            – Jan
            Feb 27 '18 at 9:30











          • No this is wrong! Trim only remove chars from the start and end of a string not in the middle

            – userSteve
            Jan 10 at 15:39











          • Yup, I was wrong on this one, good job

            – MrSmudge
            Jan 11 at 16:43



















          0














          How about BOM, the magical character Microsoft adds to start of files (at least XML)?






          share|improve this answer
























          • The wording on Wikipedia here is quite unfortunate; the BOM is not a character in this context. And what is your question exactly? :)

            – bzlm
            Oct 5 '10 at 11:36











          • @bzlm "how about..." ...

            – onemach
            Feb 21 '12 at 8:20











          • @onemach, so, whether myString.Replace ('c', '') could be achieved by myString.Replace ('c', UTF_BOM). Then I'd say the answer is "how not about...".

            – bzlm
            Feb 21 '12 at 9:38



















          0














          if you want to elliminate the empty char in string the following will work, just convert to any datatype representation you want. thanks,



          private void button1_Click(object sender, EventArgs e)
          {

          Int32 i;

          String name;

          Int32 array_number = new int[100];

          name = "1 3 5 17 8 9 6";

          name = name.Replace(' ', 'x');

          char chr = name.ToCharArray();


          for (i = 0; i < name.Length; i++)
          {
          if ((chr[i] != 'x'))
          {
          array_number[i] = Convert.ToInt32(chr[i].ToString());
          MessageBox.Show(array_number[i].ToString());
          }

          }

          }





          share|improve this answer

































            0














            In terms of C# language, the following may not make much sense. And this is not a direct answer to the question. But fowlloing is what I did in one of my business scenario



                    char? myCharFromUI = Convert.ToChar(" ");
            string myStringForDatabaseInsert = myCharFromUI.ToString().Trim();
            if (String.IsNullOrEmpty(myStringForDatabaseInsert.Trim()))
            {
            Console.Write("Success");
            }


            The null and white space had different business flows in my project. While inserting into database, I need to insert empty string to the database if it is white space.






            share|improve this answer































              0














              I know this one is pretty old, but I encountered an issue recently with having to do multiple replacements to make a file name safe. First, in the latest .NET string.Replace function null is the equivalent to empty character. Having said that, what is missing from .Net is a simple replace all that will replace any character in an array with the desired character. Please feel free to reference the code below (runs in LinqPad for testing).



              // LinqPad .ReplaceAll and SafeFileName
              void Main()
              {

              ("a:B:C").Replace(":", "_").Dump(); // can only replace 1 character for one character => a_B_C
              ("a:B:C").Replace(":", null).Dump(); // null replaces with empty => aBC
              ("a:B*C").Replace(":", null).Replace("*",null).Dump(); // Have to chain for multiples

              // Need a ReplaceAll, so I don't have to chain calls


              ("abc/123.txt").SafeFileName().Dump();
              ("abc/1/2/3.txt").SafeFileName().Dump();
              ("a:bc/1/2/3.txt").SafeFileName().Dump();
              ("a:bc/1/2/3.txt").SafeFileName('_').Dump();
              //("abc/123").SafeFileName(':').Dump(); // Throws exception as expected

              }


              static class StringExtensions
              {

              public static string SafeFileName(this string value, char? replacement = null)
              {
              return value.ReplaceAll(replacement, ':','*','?','"','<','>', '|', '/', '\');
              }

              public static string ReplaceAll(this string value, char? replacement, params char charsToGo){

              if(replacement.HasValue == false){
              return string.Join("", value.AsEnumerable().Where(x => charsToGo.Contains(x) == false));
              }
              else{

              if(charsToGo.Contains(replacement.Value)){
              throw new ArgumentException(string.Format("Replacement '{0}' is invalid. ", replacement), "replacement");
              }

              return string.Join("", value.AsEnumerable().Select(x => charsToGo.Contains(x) == true ? replacement : x));
              }

              }

              }





              share|improve this answer































                0














                If you want to remove characters that satisfy a specific condition, you may use this:



                string s = "SoMEthInG";
                s = new string(s.ToCharArray().Where(c => char.IsUpper(c)).ToArray());


                (This will leave only the uppercase characters in the string.)



                In other words, you may convert the string to an IEnumerable<char>, make changes on it and then convert it back to a string as shown above.



                Again, this enables to not only remove a specific char because of the lambda expression, although you can do so if you change the lambda expression like this: c => c != 't'.






                share|improve this answer































                  -1














                  use



                  myString.Replace ("c", "")





                  share|improve this answer
























                  • This is a duplicate of an answer from 2013 by Ian Grainger.

                    – Joe Gayetty
                    Apr 17 '18 at 14:34











                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3670505%2fwhy-is-there-no-char-empty-like-string-empty%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  19 Answers
                  19






                  active

                  oldest

                  votes








                  19 Answers
                  19






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  234














                  There's no such thing as an empty char. The closest you can get is '', the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse "" and " "" arguments don't apply for ''.



                  If you could give an example of where you'd want to use it and why you think it would be better, that might help...






                  share|improve this answer





















                  • 2





                    Isn't the the 'end of the byte array'-character? Or am I confusing with something else?

                    – Bertvan
                    Sep 8 '10 at 17:47






                  • 4





                    @Bertvan: Why would there be a character at the end of a byte array? It's used for "null terminated" strings though, yes.

                    – Jon Skeet
                    Sep 8 '10 at 17:48






                  • 25





                    Char.MinValue is better than ''

                    – Aliostad
                    Sep 8 '10 at 17:50






                  • 8





                    @Aliostad: Out of interest, if there was a similar field for Int32.Zero, would you use that instead of the literal 0? If not, what's the difference here?

                    – Jon Skeet
                    Sep 8 '10 at 18:17






                  • 7





                    @Adam, @Jon -- what is the code for bell? Or backspace better, think think... Or maybe instead of thinking it is just better to write Char.Backspace? Another reason -- you say it is better to write '0' for terminator, instead, say Char.Terminator, however it is not -- it is too easy to make a typo (fully compiled, see above), but try to write Char.Termnator. There are enough reasons for me to avoid non-checkable, raw values (space missions failed because of stupid typos like that).

                    – greenoldman
                    Sep 9 '10 at 6:19


















                  234














                  There's no such thing as an empty char. The closest you can get is '', the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse "" and " "" arguments don't apply for ''.



                  If you could give an example of where you'd want to use it and why you think it would be better, that might help...






                  share|improve this answer





















                  • 2





                    Isn't the the 'end of the byte array'-character? Or am I confusing with something else?

                    – Bertvan
                    Sep 8 '10 at 17:47






                  • 4





                    @Bertvan: Why would there be a character at the end of a byte array? It's used for "null terminated" strings though, yes.

                    – Jon Skeet
                    Sep 8 '10 at 17:48






                  • 25





                    Char.MinValue is better than ''

                    – Aliostad
                    Sep 8 '10 at 17:50






                  • 8





                    @Aliostad: Out of interest, if there was a similar field for Int32.Zero, would you use that instead of the literal 0? If not, what's the difference here?

                    – Jon Skeet
                    Sep 8 '10 at 18:17






                  • 7





                    @Adam, @Jon -- what is the code for bell? Or backspace better, think think... Or maybe instead of thinking it is just better to write Char.Backspace? Another reason -- you say it is better to write '0' for terminator, instead, say Char.Terminator, however it is not -- it is too easy to make a typo (fully compiled, see above), but try to write Char.Termnator. There are enough reasons for me to avoid non-checkable, raw values (space missions failed because of stupid typos like that).

                    – greenoldman
                    Sep 9 '10 at 6:19
















                  234












                  234








                  234







                  There's no such thing as an empty char. The closest you can get is '', the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse "" and " "" arguments don't apply for ''.



                  If you could give an example of where you'd want to use it and why you think it would be better, that might help...






                  share|improve this answer















                  There's no such thing as an empty char. The closest you can get is '', the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse "" and " "" arguments don't apply for ''.



                  If you could give an example of where you'd want to use it and why you think it would be better, that might help...







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 27 '16 at 19:32









                  samis

                  3,49362151




                  3,49362151










                  answered Sep 8 '10 at 17:45









                  Jon SkeetJon Skeet

                  1090k69179518446




                  1090k69179518446








                  • 2





                    Isn't the the 'end of the byte array'-character? Or am I confusing with something else?

                    – Bertvan
                    Sep 8 '10 at 17:47






                  • 4





                    @Bertvan: Why would there be a character at the end of a byte array? It's used for "null terminated" strings though, yes.

                    – Jon Skeet
                    Sep 8 '10 at 17:48






                  • 25





                    Char.MinValue is better than ''

                    – Aliostad
                    Sep 8 '10 at 17:50






                  • 8





                    @Aliostad: Out of interest, if there was a similar field for Int32.Zero, would you use that instead of the literal 0? If not, what's the difference here?

                    – Jon Skeet
                    Sep 8 '10 at 18:17






                  • 7





                    @Adam, @Jon -- what is the code for bell? Or backspace better, think think... Or maybe instead of thinking it is just better to write Char.Backspace? Another reason -- you say it is better to write '0' for terminator, instead, say Char.Terminator, however it is not -- it is too easy to make a typo (fully compiled, see above), but try to write Char.Termnator. There are enough reasons for me to avoid non-checkable, raw values (space missions failed because of stupid typos like that).

                    – greenoldman
                    Sep 9 '10 at 6:19
















                  • 2





                    Isn't the the 'end of the byte array'-character? Or am I confusing with something else?

                    – Bertvan
                    Sep 8 '10 at 17:47






                  • 4





                    @Bertvan: Why would there be a character at the end of a byte array? It's used for "null terminated" strings though, yes.

                    – Jon Skeet
                    Sep 8 '10 at 17:48






                  • 25





                    Char.MinValue is better than ''

                    – Aliostad
                    Sep 8 '10 at 17:50






                  • 8





                    @Aliostad: Out of interest, if there was a similar field for Int32.Zero, would you use that instead of the literal 0? If not, what's the difference here?

                    – Jon Skeet
                    Sep 8 '10 at 18:17






                  • 7





                    @Adam, @Jon -- what is the code for bell? Or backspace better, think think... Or maybe instead of thinking it is just better to write Char.Backspace? Another reason -- you say it is better to write '0' for terminator, instead, say Char.Terminator, however it is not -- it is too easy to make a typo (fully compiled, see above), but try to write Char.Termnator. There are enough reasons for me to avoid non-checkable, raw values (space missions failed because of stupid typos like that).

                    – greenoldman
                    Sep 9 '10 at 6:19










                  2




                  2





                  Isn't the the 'end of the byte array'-character? Or am I confusing with something else?

                  – Bertvan
                  Sep 8 '10 at 17:47





                  Isn't the the 'end of the byte array'-character? Or am I confusing with something else?

                  – Bertvan
                  Sep 8 '10 at 17:47




                  4




                  4





                  @Bertvan: Why would there be a character at the end of a byte array? It's used for "null terminated" strings though, yes.

                  – Jon Skeet
                  Sep 8 '10 at 17:48





                  @Bertvan: Why would there be a character at the end of a byte array? It's used for "null terminated" strings though, yes.

                  – Jon Skeet
                  Sep 8 '10 at 17:48




                  25




                  25





                  Char.MinValue is better than ''

                  – Aliostad
                  Sep 8 '10 at 17:50





                  Char.MinValue is better than ''

                  – Aliostad
                  Sep 8 '10 at 17:50




                  8




                  8





                  @Aliostad: Out of interest, if there was a similar field for Int32.Zero, would you use that instead of the literal 0? If not, what's the difference here?

                  – Jon Skeet
                  Sep 8 '10 at 18:17





                  @Aliostad: Out of interest, if there was a similar field for Int32.Zero, would you use that instead of the literal 0? If not, what's the difference here?

                  – Jon Skeet
                  Sep 8 '10 at 18:17




                  7




                  7





                  @Adam, @Jon -- what is the code for bell? Or backspace better, think think... Or maybe instead of thinking it is just better to write Char.Backspace? Another reason -- you say it is better to write '0' for terminator, instead, say Char.Terminator, however it is not -- it is too easy to make a typo (fully compiled, see above), but try to write Char.Termnator. There are enough reasons for me to avoid non-checkable, raw values (space missions failed because of stupid typos like that).

                  – greenoldman
                  Sep 9 '10 at 6:19







                  @Adam, @Jon -- what is the code for bell? Or backspace better, think think... Or maybe instead of thinking it is just better to write Char.Backspace? Another reason -- you say it is better to write '0' for terminator, instead, say Char.Terminator, however it is not -- it is too easy to make a typo (fully compiled, see above), but try to write Char.Termnator. There are enough reasons for me to avoid non-checkable, raw values (space missions failed because of stupid typos like that).

                  – greenoldman
                  Sep 9 '10 at 6:19















                  73














                  A char, unlike a string, is a discrete thing with a fixed size. A string is really a container of chars.



                  So, Char.Empty doesn't really make sense in that context. If you have a char, it's not empty.






                  share|improve this answer



















                  • 2





                    Exactly right. It makes sense to ask if a container is empty or not. It makes no sense to ask of a int or float or char is empty.

                    – T.E.D.
                    Sep 8 '10 at 18:10






                  • 1





                    @Joe: Then how can a string be empty if a string is a collection of (non-empty) chars? Probably stupid, sorry...

                    – user1477388
                    Jan 29 '13 at 16:32








                  • 6





                    Because a string isn't the individual objects, it's the collection. Think of a bucket of rocks. I can't have an empty rock. But I can have an empty bucket.

                    – Joe
                    Jan 29 '13 at 17:42






                  • 1





                    I would phrase it as "a char is a primitive, value type, and a string is non-primitive, reference type".

                    – samis
                    Aug 26 '13 at 14:26






                  • 2





                    This is the real answer.

                    – Gandalf458
                    Aug 10 '17 at 21:48
















                  73














                  A char, unlike a string, is a discrete thing with a fixed size. A string is really a container of chars.



                  So, Char.Empty doesn't really make sense in that context. If you have a char, it's not empty.






                  share|improve this answer



















                  • 2





                    Exactly right. It makes sense to ask if a container is empty or not. It makes no sense to ask of a int or float or char is empty.

                    – T.E.D.
                    Sep 8 '10 at 18:10






                  • 1





                    @Joe: Then how can a string be empty if a string is a collection of (non-empty) chars? Probably stupid, sorry...

                    – user1477388
                    Jan 29 '13 at 16:32








                  • 6





                    Because a string isn't the individual objects, it's the collection. Think of a bucket of rocks. I can't have an empty rock. But I can have an empty bucket.

                    – Joe
                    Jan 29 '13 at 17:42






                  • 1





                    I would phrase it as "a char is a primitive, value type, and a string is non-primitive, reference type".

                    – samis
                    Aug 26 '13 at 14:26






                  • 2





                    This is the real answer.

                    – Gandalf458
                    Aug 10 '17 at 21:48














                  73












                  73








                  73







                  A char, unlike a string, is a discrete thing with a fixed size. A string is really a container of chars.



                  So, Char.Empty doesn't really make sense in that context. If you have a char, it's not empty.






                  share|improve this answer













                  A char, unlike a string, is a discrete thing with a fixed size. A string is really a container of chars.



                  So, Char.Empty doesn't really make sense in that context. If you have a char, it's not empty.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 8 '10 at 17:46









                  JoeJoe

                  31.6k1390107




                  31.6k1390107








                  • 2





                    Exactly right. It makes sense to ask if a container is empty or not. It makes no sense to ask of a int or float or char is empty.

                    – T.E.D.
                    Sep 8 '10 at 18:10






                  • 1





                    @Joe: Then how can a string be empty if a string is a collection of (non-empty) chars? Probably stupid, sorry...

                    – user1477388
                    Jan 29 '13 at 16:32








                  • 6





                    Because a string isn't the individual objects, it's the collection. Think of a bucket of rocks. I can't have an empty rock. But I can have an empty bucket.

                    – Joe
                    Jan 29 '13 at 17:42






                  • 1





                    I would phrase it as "a char is a primitive, value type, and a string is non-primitive, reference type".

                    – samis
                    Aug 26 '13 at 14:26






                  • 2





                    This is the real answer.

                    – Gandalf458
                    Aug 10 '17 at 21:48














                  • 2





                    Exactly right. It makes sense to ask if a container is empty or not. It makes no sense to ask of a int or float or char is empty.

                    – T.E.D.
                    Sep 8 '10 at 18:10






                  • 1





                    @Joe: Then how can a string be empty if a string is a collection of (non-empty) chars? Probably stupid, sorry...

                    – user1477388
                    Jan 29 '13 at 16:32








                  • 6





                    Because a string isn't the individual objects, it's the collection. Think of a bucket of rocks. I can't have an empty rock. But I can have an empty bucket.

                    – Joe
                    Jan 29 '13 at 17:42






                  • 1





                    I would phrase it as "a char is a primitive, value type, and a string is non-primitive, reference type".

                    – samis
                    Aug 26 '13 at 14:26






                  • 2





                    This is the real answer.

                    – Gandalf458
                    Aug 10 '17 at 21:48








                  2




                  2





                  Exactly right. It makes sense to ask if a container is empty or not. It makes no sense to ask of a int or float or char is empty.

                  – T.E.D.
                  Sep 8 '10 at 18:10





                  Exactly right. It makes sense to ask if a container is empty or not. It makes no sense to ask of a int or float or char is empty.

                  – T.E.D.
                  Sep 8 '10 at 18:10




                  1




                  1





                  @Joe: Then how can a string be empty if a string is a collection of (non-empty) chars? Probably stupid, sorry...

                  – user1477388
                  Jan 29 '13 at 16:32







                  @Joe: Then how can a string be empty if a string is a collection of (non-empty) chars? Probably stupid, sorry...

                  – user1477388
                  Jan 29 '13 at 16:32






                  6




                  6





                  Because a string isn't the individual objects, it's the collection. Think of a bucket of rocks. I can't have an empty rock. But I can have an empty bucket.

                  – Joe
                  Jan 29 '13 at 17:42





                  Because a string isn't the individual objects, it's the collection. Think of a bucket of rocks. I can't have an empty rock. But I can have an empty bucket.

                  – Joe
                  Jan 29 '13 at 17:42




                  1




                  1





                  I would phrase it as "a char is a primitive, value type, and a string is non-primitive, reference type".

                  – samis
                  Aug 26 '13 at 14:26





                  I would phrase it as "a char is a primitive, value type, and a string is non-primitive, reference type".

                  – samis
                  Aug 26 '13 at 14:26




                  2




                  2





                  This is the real answer.

                  – Gandalf458
                  Aug 10 '17 at 21:48





                  This is the real answer.

                  – Gandalf458
                  Aug 10 '17 at 21:48











                  28














                  There's no such thing as an empty character. It always contains something. Even '' is a character.






                  share|improve this answer



















                  • 6





                    +1 nice channeling of sir jon!

                    – kenny
                    Sep 8 '10 at 19:20
















                  28














                  There's no such thing as an empty character. It always contains something. Even '' is a character.






                  share|improve this answer



















                  • 6





                    +1 nice channeling of sir jon!

                    – kenny
                    Sep 8 '10 at 19:20














                  28












                  28








                  28







                  There's no such thing as an empty character. It always contains something. Even '' is a character.






                  share|improve this answer













                  There's no such thing as an empty character. It always contains something. Even '' is a character.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 8 '10 at 17:46









                  Philippe LeybaertPhilippe Leybaert

                  132k26186211




                  132k26186211








                  • 6





                    +1 nice channeling of sir jon!

                    – kenny
                    Sep 8 '10 at 19:20














                  • 6





                    +1 nice channeling of sir jon!

                    – kenny
                    Sep 8 '10 at 19:20








                  6




                  6





                  +1 nice channeling of sir jon!

                  – kenny
                  Sep 8 '10 at 19:20





                  +1 nice channeling of sir jon!

                  – kenny
                  Sep 8 '10 at 19:20











                  22














                  Use Char.MinValue which works the same as ''. But be careful it is not the same as String.Empty.






                  share|improve this answer


























                  • Thanks, haven't seen that before. Do you know if it work in myString.Replace('c', Char.MinValue)? I should give it a try.

                    – Joan Venge
                    Sep 8 '10 at 17:50
















                  22














                  Use Char.MinValue which works the same as ''. But be careful it is not the same as String.Empty.






                  share|improve this answer


























                  • Thanks, haven't seen that before. Do you know if it work in myString.Replace('c', Char.MinValue)? I should give it a try.

                    – Joan Venge
                    Sep 8 '10 at 17:50














                  22












                  22








                  22







                  Use Char.MinValue which works the same as ''. But be careful it is not the same as String.Empty.






                  share|improve this answer















                  Use Char.MinValue which works the same as ''. But be careful it is not the same as String.Empty.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 28 '11 at 11:08

























                  answered Sep 8 '10 at 17:48









                  AliostadAliostad

                  69.7k15135190




                  69.7k15135190













                  • Thanks, haven't seen that before. Do you know if it work in myString.Replace('c', Char.MinValue)? I should give it a try.

                    – Joan Venge
                    Sep 8 '10 at 17:50



















                  • Thanks, haven't seen that before. Do you know if it work in myString.Replace('c', Char.MinValue)? I should give it a try.

                    – Joan Venge
                    Sep 8 '10 at 17:50

















                  Thanks, haven't seen that before. Do you know if it work in myString.Replace('c', Char.MinValue)? I should give it a try.

                  – Joan Venge
                  Sep 8 '10 at 17:50





                  Thanks, haven't seen that before. Do you know if it work in myString.Replace('c', Char.MinValue)? I should give it a try.

                  – Joan Venge
                  Sep 8 '10 at 17:50











                  19














                  You could use nullable chars.



                  char? c





                  share|improve this answer
























                  • This allows ''? Or just null?

                    – Joan Venge
                    Sep 8 '10 at 17:57











                  • In your case, you could do this: myString.Replace("c", (c == null ? "" : c.ToString()))

                    – paquetp
                    Sep 8 '10 at 18:13
















                  19














                  You could use nullable chars.



                  char? c





                  share|improve this answer
























                  • This allows ''? Or just null?

                    – Joan Venge
                    Sep 8 '10 at 17:57











                  • In your case, you could do this: myString.Replace("c", (c == null ? "" : c.ToString()))

                    – paquetp
                    Sep 8 '10 at 18:13














                  19












                  19








                  19







                  You could use nullable chars.



                  char? c





                  share|improve this answer













                  You could use nullable chars.



                  char? c






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 8 '10 at 17:54









                  paquetppaquetp

                  1,4581018




                  1,4581018













                  • This allows ''? Or just null?

                    – Joan Venge
                    Sep 8 '10 at 17:57











                  • In your case, you could do this: myString.Replace("c", (c == null ? "" : c.ToString()))

                    – paquetp
                    Sep 8 '10 at 18:13



















                  • This allows ''? Or just null?

                    – Joan Venge
                    Sep 8 '10 at 17:57











                  • In your case, you could do this: myString.Replace("c", (c == null ? "" : c.ToString()))

                    – paquetp
                    Sep 8 '10 at 18:13

















                  This allows ''? Or just null?

                  – Joan Venge
                  Sep 8 '10 at 17:57





                  This allows ''? Or just null?

                  – Joan Venge
                  Sep 8 '10 at 17:57













                  In your case, you could do this: myString.Replace("c", (c == null ? "" : c.ToString()))

                  – paquetp
                  Sep 8 '10 at 18:13





                  In your case, you could do this: myString.Replace("c", (c == null ? "" : c.ToString()))

                  – paquetp
                  Sep 8 '10 at 18:13











                  10














                  If you don't need the entire string, you can take advantage of the delayed execution:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChar(this IEnumerable<char> originalString, char removingChar)
                  {
                  return originalString.Where(@char => @char != removingChar);
                  }
                  }


                  You can even combine multiple characters...



                  string veryLongText = "abcdefghijk...";

                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChar('c')
                  .RemoveChar('d')
                  .Take(5);


                  ... and only the first 7 characters will be evaluated :)



                  EDIT: or, even better:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChars(this IEnumerable<char> originalString,
                  params char removingChars)
                  {
                  return originalString.Except(removingChars);
                  }
                  }


                  and its usage:



                          var veryLongText = "abcdefghijk...";
                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChars('c', 'd')
                  .Take(5)
                  .ToArray(); //to prevent multiple execution of "RemoveChars"





                  share|improve this answer





















                  • 1





                    Genius example.

                    – Joan Venge
                    Sep 8 '10 at 19:41











                  • @Joan: thanks... even if "Genius" it's a bit exaggerated :P (I don't know about its performances when removingChars will become a big array...)

                    – Notoriousxl
                    Sep 8 '10 at 19:59






                  • 1





                    Yesterday I forgot: pay attention on how you are using the result variable "firstFiveCharsWithoutCsAndDs". If you don't want to pass it to another "yield" method (like those of LINQ), call immediately a ".ToArray()" after the "Take(5)"... otherwise, the "RemoveChars + Take" chain will be executed every time you access the variable in a "traditional" fashion (for example, every you call a "Count()" on it, or when you traverse it in a foreach without "yield return")

                    – Notoriousxl
                    Sep 9 '10 at 19:07











                  • +1 nice thinking. but this can't get as maintainable or efficient as the basic approach :)

                    – nawfal
                    Feb 5 '13 at 9:51








                  • 1





                    @nawfal efficiency-wise you're right, but I think that myString.Except("c") is more declarative than myString.Replace('c', '') :P (and it scales pretty well: myString.Except("aeiou"))

                    – Notoriousxl
                    Feb 5 '13 at 18:28
















                  10














                  If you don't need the entire string, you can take advantage of the delayed execution:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChar(this IEnumerable<char> originalString, char removingChar)
                  {
                  return originalString.Where(@char => @char != removingChar);
                  }
                  }


                  You can even combine multiple characters...



                  string veryLongText = "abcdefghijk...";

                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChar('c')
                  .RemoveChar('d')
                  .Take(5);


                  ... and only the first 7 characters will be evaluated :)



                  EDIT: or, even better:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChars(this IEnumerable<char> originalString,
                  params char removingChars)
                  {
                  return originalString.Except(removingChars);
                  }
                  }


                  and its usage:



                          var veryLongText = "abcdefghijk...";
                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChars('c', 'd')
                  .Take(5)
                  .ToArray(); //to prevent multiple execution of "RemoveChars"





                  share|improve this answer





















                  • 1





                    Genius example.

                    – Joan Venge
                    Sep 8 '10 at 19:41











                  • @Joan: thanks... even if "Genius" it's a bit exaggerated :P (I don't know about its performances when removingChars will become a big array...)

                    – Notoriousxl
                    Sep 8 '10 at 19:59






                  • 1





                    Yesterday I forgot: pay attention on how you are using the result variable "firstFiveCharsWithoutCsAndDs". If you don't want to pass it to another "yield" method (like those of LINQ), call immediately a ".ToArray()" after the "Take(5)"... otherwise, the "RemoveChars + Take" chain will be executed every time you access the variable in a "traditional" fashion (for example, every you call a "Count()" on it, or when you traverse it in a foreach without "yield return")

                    – Notoriousxl
                    Sep 9 '10 at 19:07











                  • +1 nice thinking. but this can't get as maintainable or efficient as the basic approach :)

                    – nawfal
                    Feb 5 '13 at 9:51








                  • 1





                    @nawfal efficiency-wise you're right, but I think that myString.Except("c") is more declarative than myString.Replace('c', '') :P (and it scales pretty well: myString.Except("aeiou"))

                    – Notoriousxl
                    Feb 5 '13 at 18:28














                  10












                  10








                  10







                  If you don't need the entire string, you can take advantage of the delayed execution:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChar(this IEnumerable<char> originalString, char removingChar)
                  {
                  return originalString.Where(@char => @char != removingChar);
                  }
                  }


                  You can even combine multiple characters...



                  string veryLongText = "abcdefghijk...";

                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChar('c')
                  .RemoveChar('d')
                  .Take(5);


                  ... and only the first 7 characters will be evaluated :)



                  EDIT: or, even better:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChars(this IEnumerable<char> originalString,
                  params char removingChars)
                  {
                  return originalString.Except(removingChars);
                  }
                  }


                  and its usage:



                          var veryLongText = "abcdefghijk...";
                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChars('c', 'd')
                  .Take(5)
                  .ToArray(); //to prevent multiple execution of "RemoveChars"





                  share|improve this answer















                  If you don't need the entire string, you can take advantage of the delayed execution:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChar(this IEnumerable<char> originalString, char removingChar)
                  {
                  return originalString.Where(@char => @char != removingChar);
                  }
                  }


                  You can even combine multiple characters...



                  string veryLongText = "abcdefghijk...";

                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChar('c')
                  .RemoveChar('d')
                  .Take(5);


                  ... and only the first 7 characters will be evaluated :)



                  EDIT: or, even better:



                  public static class StringExtensions
                  {
                  public static IEnumerable<char> RemoveChars(this IEnumerable<char> originalString,
                  params char removingChars)
                  {
                  return originalString.Except(removingChars);
                  }
                  }


                  and its usage:



                          var veryLongText = "abcdefghijk...";
                  IEnumerable<char> firstFiveCharsWithoutCsAndDs = veryLongText
                  .RemoveChars('c', 'd')
                  .Take(5)
                  .ToArray(); //to prevent multiple execution of "RemoveChars"






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 9 '10 at 19:08

























                  answered Sep 8 '10 at 19:17









                  NotoriousxlNotoriousxl

                  9951224




                  9951224








                  • 1





                    Genius example.

                    – Joan Venge
                    Sep 8 '10 at 19:41











                  • @Joan: thanks... even if "Genius" it's a bit exaggerated :P (I don't know about its performances when removingChars will become a big array...)

                    – Notoriousxl
                    Sep 8 '10 at 19:59






                  • 1





                    Yesterday I forgot: pay attention on how you are using the result variable "firstFiveCharsWithoutCsAndDs". If you don't want to pass it to another "yield" method (like those of LINQ), call immediately a ".ToArray()" after the "Take(5)"... otherwise, the "RemoveChars + Take" chain will be executed every time you access the variable in a "traditional" fashion (for example, every you call a "Count()" on it, or when you traverse it in a foreach without "yield return")

                    – Notoriousxl
                    Sep 9 '10 at 19:07











                  • +1 nice thinking. but this can't get as maintainable or efficient as the basic approach :)

                    – nawfal
                    Feb 5 '13 at 9:51








                  • 1





                    @nawfal efficiency-wise you're right, but I think that myString.Except("c") is more declarative than myString.Replace('c', '') :P (and it scales pretty well: myString.Except("aeiou"))

                    – Notoriousxl
                    Feb 5 '13 at 18:28














                  • 1





                    Genius example.

                    – Joan Venge
                    Sep 8 '10 at 19:41











                  • @Joan: thanks... even if "Genius" it's a bit exaggerated :P (I don't know about its performances when removingChars will become a big array...)

                    – Notoriousxl
                    Sep 8 '10 at 19:59






                  • 1





                    Yesterday I forgot: pay attention on how you are using the result variable "firstFiveCharsWithoutCsAndDs". If you don't want to pass it to another "yield" method (like those of LINQ), call immediately a ".ToArray()" after the "Take(5)"... otherwise, the "RemoveChars + Take" chain will be executed every time you access the variable in a "traditional" fashion (for example, every you call a "Count()" on it, or when you traverse it in a foreach without "yield return")

                    – Notoriousxl
                    Sep 9 '10 at 19:07











                  • +1 nice thinking. but this can't get as maintainable or efficient as the basic approach :)

                    – nawfal
                    Feb 5 '13 at 9:51








                  • 1





                    @nawfal efficiency-wise you're right, but I think that myString.Except("c") is more declarative than myString.Replace('c', '') :P (and it scales pretty well: myString.Except("aeiou"))

                    – Notoriousxl
                    Feb 5 '13 at 18:28








                  1




                  1





                  Genius example.

                  – Joan Venge
                  Sep 8 '10 at 19:41





                  Genius example.

                  – Joan Venge
                  Sep 8 '10 at 19:41













                  @Joan: thanks... even if "Genius" it's a bit exaggerated :P (I don't know about its performances when removingChars will become a big array...)

                  – Notoriousxl
                  Sep 8 '10 at 19:59





                  @Joan: thanks... even if "Genius" it's a bit exaggerated :P (I don't know about its performances when removingChars will become a big array...)

                  – Notoriousxl
                  Sep 8 '10 at 19:59




                  1




                  1





                  Yesterday I forgot: pay attention on how you are using the result variable "firstFiveCharsWithoutCsAndDs". If you don't want to pass it to another "yield" method (like those of LINQ), call immediately a ".ToArray()" after the "Take(5)"... otherwise, the "RemoveChars + Take" chain will be executed every time you access the variable in a "traditional" fashion (for example, every you call a "Count()" on it, or when you traverse it in a foreach without "yield return")

                  – Notoriousxl
                  Sep 9 '10 at 19:07





                  Yesterday I forgot: pay attention on how you are using the result variable "firstFiveCharsWithoutCsAndDs". If you don't want to pass it to another "yield" method (like those of LINQ), call immediately a ".ToArray()" after the "Take(5)"... otherwise, the "RemoveChars + Take" chain will be executed every time you access the variable in a "traditional" fashion (for example, every you call a "Count()" on it, or when you traverse it in a foreach without "yield return")

                  – Notoriousxl
                  Sep 9 '10 at 19:07













                  +1 nice thinking. but this can't get as maintainable or efficient as the basic approach :)

                  – nawfal
                  Feb 5 '13 at 9:51







                  +1 nice thinking. but this can't get as maintainable or efficient as the basic approach :)

                  – nawfal
                  Feb 5 '13 at 9:51






                  1




                  1





                  @nawfal efficiency-wise you're right, but I think that myString.Except("c") is more declarative than myString.Replace('c', '') :P (and it scales pretty well: myString.Except("aeiou"))

                  – Notoriousxl
                  Feb 5 '13 at 18:28





                  @nawfal efficiency-wise you're right, but I think that myString.Except("c") is more declarative than myString.Replace('c', '') :P (and it scales pretty well: myString.Except("aeiou"))

                  – Notoriousxl
                  Feb 5 '13 at 18:28











                  5














                  the same reason there isn't an int.Empty. Containers can be empty. Scalar values cannot be. If you mean 0 (which is not empty), then use ''. If you mean null, then use null :)






                  share|improve this answer



















                  • 2





                    null is not possible as char is a ValueType. You'd have to use char? to be able to assign null to it.

                    – Femaref
                    Sep 8 '10 at 17:53











                  • you chould make it nullable. see my answer

                    – paquetp
                    Sep 8 '10 at 17:54











                  • Good point man.

                    – Joan Venge
                    Sep 8 '10 at 17:56
















                  5














                  the same reason there isn't an int.Empty. Containers can be empty. Scalar values cannot be. If you mean 0 (which is not empty), then use ''. If you mean null, then use null :)






                  share|improve this answer



















                  • 2





                    null is not possible as char is a ValueType. You'd have to use char? to be able to assign null to it.

                    – Femaref
                    Sep 8 '10 at 17:53











                  • you chould make it nullable. see my answer

                    – paquetp
                    Sep 8 '10 at 17:54











                  • Good point man.

                    – Joan Venge
                    Sep 8 '10 at 17:56














                  5












                  5








                  5







                  the same reason there isn't an int.Empty. Containers can be empty. Scalar values cannot be. If you mean 0 (which is not empty), then use ''. If you mean null, then use null :)






                  share|improve this answer













                  the same reason there isn't an int.Empty. Containers can be empty. Scalar values cannot be. If you mean 0 (which is not empty), then use ''. If you mean null, then use null :)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 8 '10 at 17:51









                  tenfourtenfour

                  27.1k1259121




                  27.1k1259121








                  • 2





                    null is not possible as char is a ValueType. You'd have to use char? to be able to assign null to it.

                    – Femaref
                    Sep 8 '10 at 17:53











                  • you chould make it nullable. see my answer

                    – paquetp
                    Sep 8 '10 at 17:54











                  • Good point man.

                    – Joan Venge
                    Sep 8 '10 at 17:56














                  • 2





                    null is not possible as char is a ValueType. You'd have to use char? to be able to assign null to it.

                    – Femaref
                    Sep 8 '10 at 17:53











                  • you chould make it nullable. see my answer

                    – paquetp
                    Sep 8 '10 at 17:54











                  • Good point man.

                    – Joan Venge
                    Sep 8 '10 at 17:56








                  2




                  2





                  null is not possible as char is a ValueType. You'd have to use char? to be able to assign null to it.

                  – Femaref
                  Sep 8 '10 at 17:53





                  null is not possible as char is a ValueType. You'd have to use char? to be able to assign null to it.

                  – Femaref
                  Sep 8 '10 at 17:53













                  you chould make it nullable. see my answer

                  – paquetp
                  Sep 8 '10 at 17:54





                  you chould make it nullable. see my answer

                  – paquetp
                  Sep 8 '10 at 17:54













                  Good point man.

                  – Joan Venge
                  Sep 8 '10 at 17:56





                  Good point man.

                  – Joan Venge
                  Sep 8 '10 at 17:56











                  5














                  A char is a value type, so its value cannot be null. (Unless it is wrapped in a Nullable container).



                  Since it can't be null, in contains some numeric code and each code is mapped to some character.






                  share|improve this answer




























                    5














                    A char is a value type, so its value cannot be null. (Unless it is wrapped in a Nullable container).



                    Since it can't be null, in contains some numeric code and each code is mapped to some character.






                    share|improve this answer


























                      5












                      5








                      5







                      A char is a value type, so its value cannot be null. (Unless it is wrapped in a Nullable container).



                      Since it can't be null, in contains some numeric code and each code is mapped to some character.






                      share|improve this answer













                      A char is a value type, so its value cannot be null. (Unless it is wrapped in a Nullable container).



                      Since it can't be null, in contains some numeric code and each code is mapped to some character.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 8 '10 at 17:53









                      epotterepotter

                      4,73865684




                      4,73865684























                          4














                          myString = myString.Replace('c'.ToString(), "");


                          OK, this is not particularly elegant for removing letters, since the .Replace method has an overload that takes string parameters. But this works for removing carriage returns, line feeds, tabs, etc. This example removes tab characters:



                          myString = myString.Replace('t'.ToString(), "");





                          share|improve this answer






























                            4














                            myString = myString.Replace('c'.ToString(), "");


                            OK, this is not particularly elegant for removing letters, since the .Replace method has an overload that takes string parameters. But this works for removing carriage returns, line feeds, tabs, etc. This example removes tab characters:



                            myString = myString.Replace('t'.ToString(), "");





                            share|improve this answer




























                              4












                              4








                              4







                              myString = myString.Replace('c'.ToString(), "");


                              OK, this is not particularly elegant for removing letters, since the .Replace method has an overload that takes string parameters. But this works for removing carriage returns, line feeds, tabs, etc. This example removes tab characters:



                              myString = myString.Replace('t'.ToString(), "");





                              share|improve this answer















                              myString = myString.Replace('c'.ToString(), "");


                              OK, this is not particularly elegant for removing letters, since the .Replace method has an overload that takes string parameters. But this works for removing carriage returns, line feeds, tabs, etc. This example removes tab characters:



                              myString = myString.Replace('t'.ToString(), "");






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 21 '11 at 0:26

























                              answered May 21 '11 at 0:12









                              Mike TaverneMike Taverne

                              6,05722139




                              6,05722139























                                  3














                                  Not an answer to your question, but to denote a default char you can use just



                                  default(char)


                                  which is same as char.MinValue which in turn is same as . One shouldn't use if for something like an empty string though.






                                  share|improve this answer




























                                    3














                                    Not an answer to your question, but to denote a default char you can use just



                                    default(char)


                                    which is same as char.MinValue which in turn is same as . One shouldn't use if for something like an empty string though.






                                    share|improve this answer


























                                      3












                                      3








                                      3







                                      Not an answer to your question, but to denote a default char you can use just



                                      default(char)


                                      which is same as char.MinValue which in turn is same as . One shouldn't use if for something like an empty string though.






                                      share|improve this answer













                                      Not an answer to your question, but to denote a default char you can use just



                                      default(char)


                                      which is same as char.MinValue which in turn is same as . One shouldn't use if for something like an empty string though.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 4 '13 at 9:53









                                      nawfalnawfal

                                      43.2k36255298




                                      43.2k36255298























                                          1














                                          Doesn't answer your first question - but for the specific problem you had, you can just use strings instead of chars, right?:



                                          myString.Replace("c", "")


                                          There a reason you wouldn't want to do that?






                                          share|improve this answer






























                                            1














                                            Doesn't answer your first question - but for the specific problem you had, you can just use strings instead of chars, right?:



                                            myString.Replace("c", "")


                                            There a reason you wouldn't want to do that?






                                            share|improve this answer




























                                              1












                                              1








                                              1







                                              Doesn't answer your first question - but for the specific problem you had, you can just use strings instead of chars, right?:



                                              myString.Replace("c", "")


                                              There a reason you wouldn't want to do that?






                                              share|improve this answer















                                              Doesn't answer your first question - but for the specific problem you had, you can just use strings instead of chars, right?:



                                              myString.Replace("c", "")


                                              There a reason you wouldn't want to do that?







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Jan 29 '13 at 16:32

























                                              answered Jan 29 '13 at 16:24









                                              Ian GraingerIan Grainger

                                              2,89423056




                                              2,89423056























                                                  1














                                                  You can also rebuild your string character by character, excluding the characters that you want to get rid of.



                                                  Here's an extension method to do this:



                                                      static public string RemoveAny(this string s, string charsToRemove)
                                                  {
                                                  var result = "";
                                                  foreach (var c in s)
                                                  if (charsToRemove.Contains(c))
                                                  continue;
                                                  else
                                                  result += c;

                                                  return result;
                                                  }


                                                  It's not slick or fancy, but it works well.



                                                  Use like this:



                                                  string newString = "My_String".RemoveAny("_"); //yields "MyString"





                                                  share|improve this answer
























                                                  • Use a StringBuilder for result. Why not wrap return s.Replace(charsToRemove,"");?

                                                    – aloisdg
                                                    Jul 9 '16 at 15:26


















                                                  1














                                                  You can also rebuild your string character by character, excluding the characters that you want to get rid of.



                                                  Here's an extension method to do this:



                                                      static public string RemoveAny(this string s, string charsToRemove)
                                                  {
                                                  var result = "";
                                                  foreach (var c in s)
                                                  if (charsToRemove.Contains(c))
                                                  continue;
                                                  else
                                                  result += c;

                                                  return result;
                                                  }


                                                  It's not slick or fancy, but it works well.



                                                  Use like this:



                                                  string newString = "My_String".RemoveAny("_"); //yields "MyString"





                                                  share|improve this answer
























                                                  • Use a StringBuilder for result. Why not wrap return s.Replace(charsToRemove,"");?

                                                    – aloisdg
                                                    Jul 9 '16 at 15:26
















                                                  1












                                                  1








                                                  1







                                                  You can also rebuild your string character by character, excluding the characters that you want to get rid of.



                                                  Here's an extension method to do this:



                                                      static public string RemoveAny(this string s, string charsToRemove)
                                                  {
                                                  var result = "";
                                                  foreach (var c in s)
                                                  if (charsToRemove.Contains(c))
                                                  continue;
                                                  else
                                                  result += c;

                                                  return result;
                                                  }


                                                  It's not slick or fancy, but it works well.



                                                  Use like this:



                                                  string newString = "My_String".RemoveAny("_"); //yields "MyString"





                                                  share|improve this answer













                                                  You can also rebuild your string character by character, excluding the characters that you want to get rid of.



                                                  Here's an extension method to do this:



                                                      static public string RemoveAny(this string s, string charsToRemove)
                                                  {
                                                  var result = "";
                                                  foreach (var c in s)
                                                  if (charsToRemove.Contains(c))
                                                  continue;
                                                  else
                                                  result += c;

                                                  return result;
                                                  }


                                                  It's not slick or fancy, but it works well.



                                                  Use like this:



                                                  string newString = "My_String".RemoveAny("_"); //yields "MyString"






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Sep 2 '15 at 14:12









                                                  matrixuglymatrixugly

                                                  1,43621737




                                                  1,43621737













                                                  • Use a StringBuilder for result. Why not wrap return s.Replace(charsToRemove,"");?

                                                    – aloisdg
                                                    Jul 9 '16 at 15:26





















                                                  • Use a StringBuilder for result. Why not wrap return s.Replace(charsToRemove,"");?

                                                    – aloisdg
                                                    Jul 9 '16 at 15:26



















                                                  Use a StringBuilder for result. Why not wrap return s.Replace(charsToRemove,"");?

                                                  – aloisdg
                                                  Jul 9 '16 at 15:26







                                                  Use a StringBuilder for result. Why not wrap return s.Replace(charsToRemove,"");?

                                                  – aloisdg
                                                  Jul 9 '16 at 15:26













                                                  1














                                                  Easiest way to blanket remove a character from string is to Trim it



                                                  cl = cl.Trim(' ');



                                                  Removes all of the spaces in a string






                                                  share|improve this answer
























                                                  • This is helpful if one wants to use use .Replace('c', ' ') with the downside of removing other whitespaces. But its more helpful than lots of other answers given.

                                                    – Jan
                                                    Feb 27 '18 at 9:30











                                                  • No this is wrong! Trim only remove chars from the start and end of a string not in the middle

                                                    – userSteve
                                                    Jan 10 at 15:39











                                                  • Yup, I was wrong on this one, good job

                                                    – MrSmudge
                                                    Jan 11 at 16:43
















                                                  1














                                                  Easiest way to blanket remove a character from string is to Trim it



                                                  cl = cl.Trim(' ');



                                                  Removes all of the spaces in a string






                                                  share|improve this answer
























                                                  • This is helpful if one wants to use use .Replace('c', ' ') with the downside of removing other whitespaces. But its more helpful than lots of other answers given.

                                                    – Jan
                                                    Feb 27 '18 at 9:30











                                                  • No this is wrong! Trim only remove chars from the start and end of a string not in the middle

                                                    – userSteve
                                                    Jan 10 at 15:39











                                                  • Yup, I was wrong on this one, good job

                                                    – MrSmudge
                                                    Jan 11 at 16:43














                                                  1












                                                  1








                                                  1







                                                  Easiest way to blanket remove a character from string is to Trim it



                                                  cl = cl.Trim(' ');



                                                  Removes all of the spaces in a string






                                                  share|improve this answer













                                                  Easiest way to blanket remove a character from string is to Trim it



                                                  cl = cl.Trim(' ');



                                                  Removes all of the spaces in a string







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Sep 4 '17 at 11:07









                                                  MrSmudgeMrSmudge

                                                  461




                                                  461













                                                  • This is helpful if one wants to use use .Replace('c', ' ') with the downside of removing other whitespaces. But its more helpful than lots of other answers given.

                                                    – Jan
                                                    Feb 27 '18 at 9:30











                                                  • No this is wrong! Trim only remove chars from the start and end of a string not in the middle

                                                    – userSteve
                                                    Jan 10 at 15:39











                                                  • Yup, I was wrong on this one, good job

                                                    – MrSmudge
                                                    Jan 11 at 16:43



















                                                  • This is helpful if one wants to use use .Replace('c', ' ') with the downside of removing other whitespaces. But its more helpful than lots of other answers given.

                                                    – Jan
                                                    Feb 27 '18 at 9:30











                                                  • No this is wrong! Trim only remove chars from the start and end of a string not in the middle

                                                    – userSteve
                                                    Jan 10 at 15:39











                                                  • Yup, I was wrong on this one, good job

                                                    – MrSmudge
                                                    Jan 11 at 16:43

















                                                  This is helpful if one wants to use use .Replace('c', ' ') with the downside of removing other whitespaces. But its more helpful than lots of other answers given.

                                                  – Jan
                                                  Feb 27 '18 at 9:30





                                                  This is helpful if one wants to use use .Replace('c', ' ') with the downside of removing other whitespaces. But its more helpful than lots of other answers given.

                                                  – Jan
                                                  Feb 27 '18 at 9:30













                                                  No this is wrong! Trim only remove chars from the start and end of a string not in the middle

                                                  – userSteve
                                                  Jan 10 at 15:39





                                                  No this is wrong! Trim only remove chars from the start and end of a string not in the middle

                                                  – userSteve
                                                  Jan 10 at 15:39













                                                  Yup, I was wrong on this one, good job

                                                  – MrSmudge
                                                  Jan 11 at 16:43





                                                  Yup, I was wrong on this one, good job

                                                  – MrSmudge
                                                  Jan 11 at 16:43











                                                  0














                                                  How about BOM, the magical character Microsoft adds to start of files (at least XML)?






                                                  share|improve this answer
























                                                  • The wording on Wikipedia here is quite unfortunate; the BOM is not a character in this context. And what is your question exactly? :)

                                                    – bzlm
                                                    Oct 5 '10 at 11:36











                                                  • @bzlm "how about..." ...

                                                    – onemach
                                                    Feb 21 '12 at 8:20











                                                  • @onemach, so, whether myString.Replace ('c', '') could be achieved by myString.Replace ('c', UTF_BOM). Then I'd say the answer is "how not about...".

                                                    – bzlm
                                                    Feb 21 '12 at 9:38
















                                                  0














                                                  How about BOM, the magical character Microsoft adds to start of files (at least XML)?






                                                  share|improve this answer
























                                                  • The wording on Wikipedia here is quite unfortunate; the BOM is not a character in this context. And what is your question exactly? :)

                                                    – bzlm
                                                    Oct 5 '10 at 11:36











                                                  • @bzlm "how about..." ...

                                                    – onemach
                                                    Feb 21 '12 at 8:20











                                                  • @onemach, so, whether myString.Replace ('c', '') could be achieved by myString.Replace ('c', UTF_BOM). Then I'd say the answer is "how not about...".

                                                    – bzlm
                                                    Feb 21 '12 at 9:38














                                                  0












                                                  0








                                                  0







                                                  How about BOM, the magical character Microsoft adds to start of files (at least XML)?






                                                  share|improve this answer













                                                  How about BOM, the magical character Microsoft adds to start of files (at least XML)?







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Sep 10 '10 at 5:02









                                                  Arto ViitanenArto Viitanen

                                                  1743




                                                  1743













                                                  • The wording on Wikipedia here is quite unfortunate; the BOM is not a character in this context. And what is your question exactly? :)

                                                    – bzlm
                                                    Oct 5 '10 at 11:36











                                                  • @bzlm "how about..." ...

                                                    – onemach
                                                    Feb 21 '12 at 8:20











                                                  • @onemach, so, whether myString.Replace ('c', '') could be achieved by myString.Replace ('c', UTF_BOM). Then I'd say the answer is "how not about...".

                                                    – bzlm
                                                    Feb 21 '12 at 9:38



















                                                  • The wording on Wikipedia here is quite unfortunate; the BOM is not a character in this context. And what is your question exactly? :)

                                                    – bzlm
                                                    Oct 5 '10 at 11:36











                                                  • @bzlm "how about..." ...

                                                    – onemach
                                                    Feb 21 '12 at 8:20











                                                  • @onemach, so, whether myString.Replace ('c', '') could be achieved by myString.Replace ('c', UTF_BOM). Then I'd say the answer is "how not about...".

                                                    – bzlm
                                                    Feb 21 '12 at 9:38

















                                                  The wording on Wikipedia here is quite unfortunate; the BOM is not a character in this context. And what is your question exactly? :)

                                                  – bzlm
                                                  Oct 5 '10 at 11:36





                                                  The wording on Wikipedia here is quite unfortunate; the BOM is not a character in this context. And what is your question exactly? :)

                                                  – bzlm
                                                  Oct 5 '10 at 11:36













                                                  @bzlm "how about..." ...

                                                  – onemach
                                                  Feb 21 '12 at 8:20





                                                  @bzlm "how about..." ...

                                                  – onemach
                                                  Feb 21 '12 at 8:20













                                                  @onemach, so, whether myString.Replace ('c', '') could be achieved by myString.Replace ('c', UTF_BOM). Then I'd say the answer is "how not about...".

                                                  – bzlm
                                                  Feb 21 '12 at 9:38





                                                  @onemach, so, whether myString.Replace ('c', '') could be achieved by myString.Replace ('c', UTF_BOM). Then I'd say the answer is "how not about...".

                                                  – bzlm
                                                  Feb 21 '12 at 9:38











                                                  0














                                                  if you want to elliminate the empty char in string the following will work, just convert to any datatype representation you want. thanks,



                                                  private void button1_Click(object sender, EventArgs e)
                                                  {

                                                  Int32 i;

                                                  String name;

                                                  Int32 array_number = new int[100];

                                                  name = "1 3 5 17 8 9 6";

                                                  name = name.Replace(' ', 'x');

                                                  char chr = name.ToCharArray();


                                                  for (i = 0; i < name.Length; i++)
                                                  {
                                                  if ((chr[i] != 'x'))
                                                  {
                                                  array_number[i] = Convert.ToInt32(chr[i].ToString());
                                                  MessageBox.Show(array_number[i].ToString());
                                                  }

                                                  }

                                                  }





                                                  share|improve this answer






























                                                    0














                                                    if you want to elliminate the empty char in string the following will work, just convert to any datatype representation you want. thanks,



                                                    private void button1_Click(object sender, EventArgs e)
                                                    {

                                                    Int32 i;

                                                    String name;

                                                    Int32 array_number = new int[100];

                                                    name = "1 3 5 17 8 9 6";

                                                    name = name.Replace(' ', 'x');

                                                    char chr = name.ToCharArray();


                                                    for (i = 0; i < name.Length; i++)
                                                    {
                                                    if ((chr[i] != 'x'))
                                                    {
                                                    array_number[i] = Convert.ToInt32(chr[i].ToString());
                                                    MessageBox.Show(array_number[i].ToString());
                                                    }

                                                    }

                                                    }





                                                    share|improve this answer




























                                                      0












                                                      0








                                                      0







                                                      if you want to elliminate the empty char in string the following will work, just convert to any datatype representation you want. thanks,



                                                      private void button1_Click(object sender, EventArgs e)
                                                      {

                                                      Int32 i;

                                                      String name;

                                                      Int32 array_number = new int[100];

                                                      name = "1 3 5 17 8 9 6";

                                                      name = name.Replace(' ', 'x');

                                                      char chr = name.ToCharArray();


                                                      for (i = 0; i < name.Length; i++)
                                                      {
                                                      if ((chr[i] != 'x'))
                                                      {
                                                      array_number[i] = Convert.ToInt32(chr[i].ToString());
                                                      MessageBox.Show(array_number[i].ToString());
                                                      }

                                                      }

                                                      }





                                                      share|improve this answer















                                                      if you want to elliminate the empty char in string the following will work, just convert to any datatype representation you want. thanks,



                                                      private void button1_Click(object sender, EventArgs e)
                                                      {

                                                      Int32 i;

                                                      String name;

                                                      Int32 array_number = new int[100];

                                                      name = "1 3 5 17 8 9 6";

                                                      name = name.Replace(' ', 'x');

                                                      char chr = name.ToCharArray();


                                                      for (i = 0; i < name.Length; i++)
                                                      {
                                                      if ((chr[i] != 'x'))
                                                      {
                                                      array_number[i] = Convert.ToInt32(chr[i].ToString());
                                                      MessageBox.Show(array_number[i].ToString());
                                                      }

                                                      }

                                                      }






                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Jun 27 '13 at 9:28









                                                      Joan Venge

                                                      102k174401625




                                                      102k174401625










                                                      answered Jun 26 '13 at 13:48









                                                      Alexander ZaldostanovAlexander Zaldostanov

                                                      1,93932329




                                                      1,93932329























                                                          0














                                                          In terms of C# language, the following may not make much sense. And this is not a direct answer to the question. But fowlloing is what I did in one of my business scenario



                                                                  char? myCharFromUI = Convert.ToChar(" ");
                                                          string myStringForDatabaseInsert = myCharFromUI.ToString().Trim();
                                                          if (String.IsNullOrEmpty(myStringForDatabaseInsert.Trim()))
                                                          {
                                                          Console.Write("Success");
                                                          }


                                                          The null and white space had different business flows in my project. While inserting into database, I need to insert empty string to the database if it is white space.






                                                          share|improve this answer




























                                                            0














                                                            In terms of C# language, the following may not make much sense. And this is not a direct answer to the question. But fowlloing is what I did in one of my business scenario



                                                                    char? myCharFromUI = Convert.ToChar(" ");
                                                            string myStringForDatabaseInsert = myCharFromUI.ToString().Trim();
                                                            if (String.IsNullOrEmpty(myStringForDatabaseInsert.Trim()))
                                                            {
                                                            Console.Write("Success");
                                                            }


                                                            The null and white space had different business flows in my project. While inserting into database, I need to insert empty string to the database if it is white space.






                                                            share|improve this answer


























                                                              0












                                                              0








                                                              0







                                                              In terms of C# language, the following may not make much sense. And this is not a direct answer to the question. But fowlloing is what I did in one of my business scenario



                                                                      char? myCharFromUI = Convert.ToChar(" ");
                                                              string myStringForDatabaseInsert = myCharFromUI.ToString().Trim();
                                                              if (String.IsNullOrEmpty(myStringForDatabaseInsert.Trim()))
                                                              {
                                                              Console.Write("Success");
                                                              }


                                                              The null and white space had different business flows in my project. While inserting into database, I need to insert empty string to the database if it is white space.






                                                              share|improve this answer













                                                              In terms of C# language, the following may not make much sense. And this is not a direct answer to the question. But fowlloing is what I did in one of my business scenario



                                                                      char? myCharFromUI = Convert.ToChar(" ");
                                                              string myStringForDatabaseInsert = myCharFromUI.ToString().Trim();
                                                              if (String.IsNullOrEmpty(myStringForDatabaseInsert.Trim()))
                                                              {
                                                              Console.Write("Success");
                                                              }


                                                              The null and white space had different business flows in my project. While inserting into database, I need to insert empty string to the database if it is white space.







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Sep 5 '13 at 10:16









                                                              LijoLijo

                                                              10.9k55198335




                                                              10.9k55198335























                                                                  0














                                                                  I know this one is pretty old, but I encountered an issue recently with having to do multiple replacements to make a file name safe. First, in the latest .NET string.Replace function null is the equivalent to empty character. Having said that, what is missing from .Net is a simple replace all that will replace any character in an array with the desired character. Please feel free to reference the code below (runs in LinqPad for testing).



                                                                  // LinqPad .ReplaceAll and SafeFileName
                                                                  void Main()
                                                                  {

                                                                  ("a:B:C").Replace(":", "_").Dump(); // can only replace 1 character for one character => a_B_C
                                                                  ("a:B:C").Replace(":", null).Dump(); // null replaces with empty => aBC
                                                                  ("a:B*C").Replace(":", null).Replace("*",null).Dump(); // Have to chain for multiples

                                                                  // Need a ReplaceAll, so I don't have to chain calls


                                                                  ("abc/123.txt").SafeFileName().Dump();
                                                                  ("abc/1/2/3.txt").SafeFileName().Dump();
                                                                  ("a:bc/1/2/3.txt").SafeFileName().Dump();
                                                                  ("a:bc/1/2/3.txt").SafeFileName('_').Dump();
                                                                  //("abc/123").SafeFileName(':').Dump(); // Throws exception as expected

                                                                  }


                                                                  static class StringExtensions
                                                                  {

                                                                  public static string SafeFileName(this string value, char? replacement = null)
                                                                  {
                                                                  return value.ReplaceAll(replacement, ':','*','?','"','<','>', '|', '/', '\');
                                                                  }

                                                                  public static string ReplaceAll(this string value, char? replacement, params char charsToGo){

                                                                  if(replacement.HasValue == false){
                                                                  return string.Join("", value.AsEnumerable().Where(x => charsToGo.Contains(x) == false));
                                                                  }
                                                                  else{

                                                                  if(charsToGo.Contains(replacement.Value)){
                                                                  throw new ArgumentException(string.Format("Replacement '{0}' is invalid. ", replacement), "replacement");
                                                                  }

                                                                  return string.Join("", value.AsEnumerable().Select(x => charsToGo.Contains(x) == true ? replacement : x));
                                                                  }

                                                                  }

                                                                  }





                                                                  share|improve this answer




























                                                                    0














                                                                    I know this one is pretty old, but I encountered an issue recently with having to do multiple replacements to make a file name safe. First, in the latest .NET string.Replace function null is the equivalent to empty character. Having said that, what is missing from .Net is a simple replace all that will replace any character in an array with the desired character. Please feel free to reference the code below (runs in LinqPad for testing).



                                                                    // LinqPad .ReplaceAll and SafeFileName
                                                                    void Main()
                                                                    {

                                                                    ("a:B:C").Replace(":", "_").Dump(); // can only replace 1 character for one character => a_B_C
                                                                    ("a:B:C").Replace(":", null).Dump(); // null replaces with empty => aBC
                                                                    ("a:B*C").Replace(":", null).Replace("*",null).Dump(); // Have to chain for multiples

                                                                    // Need a ReplaceAll, so I don't have to chain calls


                                                                    ("abc/123.txt").SafeFileName().Dump();
                                                                    ("abc/1/2/3.txt").SafeFileName().Dump();
                                                                    ("a:bc/1/2/3.txt").SafeFileName().Dump();
                                                                    ("a:bc/1/2/3.txt").SafeFileName('_').Dump();
                                                                    //("abc/123").SafeFileName(':').Dump(); // Throws exception as expected

                                                                    }


                                                                    static class StringExtensions
                                                                    {

                                                                    public static string SafeFileName(this string value, char? replacement = null)
                                                                    {
                                                                    return value.ReplaceAll(replacement, ':','*','?','"','<','>', '|', '/', '\');
                                                                    }

                                                                    public static string ReplaceAll(this string value, char? replacement, params char charsToGo){

                                                                    if(replacement.HasValue == false){
                                                                    return string.Join("", value.AsEnumerable().Where(x => charsToGo.Contains(x) == false));
                                                                    }
                                                                    else{

                                                                    if(charsToGo.Contains(replacement.Value)){
                                                                    throw new ArgumentException(string.Format("Replacement '{0}' is invalid. ", replacement), "replacement");
                                                                    }

                                                                    return string.Join("", value.AsEnumerable().Select(x => charsToGo.Contains(x) == true ? replacement : x));
                                                                    }

                                                                    }

                                                                    }





                                                                    share|improve this answer


























                                                                      0












                                                                      0








                                                                      0







                                                                      I know this one is pretty old, but I encountered an issue recently with having to do multiple replacements to make a file name safe. First, in the latest .NET string.Replace function null is the equivalent to empty character. Having said that, what is missing from .Net is a simple replace all that will replace any character in an array with the desired character. Please feel free to reference the code below (runs in LinqPad for testing).



                                                                      // LinqPad .ReplaceAll and SafeFileName
                                                                      void Main()
                                                                      {

                                                                      ("a:B:C").Replace(":", "_").Dump(); // can only replace 1 character for one character => a_B_C
                                                                      ("a:B:C").Replace(":", null).Dump(); // null replaces with empty => aBC
                                                                      ("a:B*C").Replace(":", null).Replace("*",null).Dump(); // Have to chain for multiples

                                                                      // Need a ReplaceAll, so I don't have to chain calls


                                                                      ("abc/123.txt").SafeFileName().Dump();
                                                                      ("abc/1/2/3.txt").SafeFileName().Dump();
                                                                      ("a:bc/1/2/3.txt").SafeFileName().Dump();
                                                                      ("a:bc/1/2/3.txt").SafeFileName('_').Dump();
                                                                      //("abc/123").SafeFileName(':').Dump(); // Throws exception as expected

                                                                      }


                                                                      static class StringExtensions
                                                                      {

                                                                      public static string SafeFileName(this string value, char? replacement = null)
                                                                      {
                                                                      return value.ReplaceAll(replacement, ':','*','?','"','<','>', '|', '/', '\');
                                                                      }

                                                                      public static string ReplaceAll(this string value, char? replacement, params char charsToGo){

                                                                      if(replacement.HasValue == false){
                                                                      return string.Join("", value.AsEnumerable().Where(x => charsToGo.Contains(x) == false));
                                                                      }
                                                                      else{

                                                                      if(charsToGo.Contains(replacement.Value)){
                                                                      throw new ArgumentException(string.Format("Replacement '{0}' is invalid. ", replacement), "replacement");
                                                                      }

                                                                      return string.Join("", value.AsEnumerable().Select(x => charsToGo.Contains(x) == true ? replacement : x));
                                                                      }

                                                                      }

                                                                      }





                                                                      share|improve this answer













                                                                      I know this one is pretty old, but I encountered an issue recently with having to do multiple replacements to make a file name safe. First, in the latest .NET string.Replace function null is the equivalent to empty character. Having said that, what is missing from .Net is a simple replace all that will replace any character in an array with the desired character. Please feel free to reference the code below (runs in LinqPad for testing).



                                                                      // LinqPad .ReplaceAll and SafeFileName
                                                                      void Main()
                                                                      {

                                                                      ("a:B:C").Replace(":", "_").Dump(); // can only replace 1 character for one character => a_B_C
                                                                      ("a:B:C").Replace(":", null).Dump(); // null replaces with empty => aBC
                                                                      ("a:B*C").Replace(":", null).Replace("*",null).Dump(); // Have to chain for multiples

                                                                      // Need a ReplaceAll, so I don't have to chain calls


                                                                      ("abc/123.txt").SafeFileName().Dump();
                                                                      ("abc/1/2/3.txt").SafeFileName().Dump();
                                                                      ("a:bc/1/2/3.txt").SafeFileName().Dump();
                                                                      ("a:bc/1/2/3.txt").SafeFileName('_').Dump();
                                                                      //("abc/123").SafeFileName(':').Dump(); // Throws exception as expected

                                                                      }


                                                                      static class StringExtensions
                                                                      {

                                                                      public static string SafeFileName(this string value, char? replacement = null)
                                                                      {
                                                                      return value.ReplaceAll(replacement, ':','*','?','"','<','>', '|', '/', '\');
                                                                      }

                                                                      public static string ReplaceAll(this string value, char? replacement, params char charsToGo){

                                                                      if(replacement.HasValue == false){
                                                                      return string.Join("", value.AsEnumerable().Where(x => charsToGo.Contains(x) == false));
                                                                      }
                                                                      else{

                                                                      if(charsToGo.Contains(replacement.Value)){
                                                                      throw new ArgumentException(string.Format("Replacement '{0}' is invalid. ", replacement), "replacement");
                                                                      }

                                                                      return string.Join("", value.AsEnumerable().Select(x => charsToGo.Contains(x) == true ? replacement : x));
                                                                      }

                                                                      }

                                                                      }






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Feb 27 '15 at 15:18









                                                                      JimJim

                                                                      7461712




                                                                      7461712























                                                                          0














                                                                          If you want to remove characters that satisfy a specific condition, you may use this:



                                                                          string s = "SoMEthInG";
                                                                          s = new string(s.ToCharArray().Where(c => char.IsUpper(c)).ToArray());


                                                                          (This will leave only the uppercase characters in the string.)



                                                                          In other words, you may convert the string to an IEnumerable<char>, make changes on it and then convert it back to a string as shown above.



                                                                          Again, this enables to not only remove a specific char because of the lambda expression, although you can do so if you change the lambda expression like this: c => c != 't'.






                                                                          share|improve this answer




























                                                                            0














                                                                            If you want to remove characters that satisfy a specific condition, you may use this:



                                                                            string s = "SoMEthInG";
                                                                            s = new string(s.ToCharArray().Where(c => char.IsUpper(c)).ToArray());


                                                                            (This will leave only the uppercase characters in the string.)



                                                                            In other words, you may convert the string to an IEnumerable<char>, make changes on it and then convert it back to a string as shown above.



                                                                            Again, this enables to not only remove a specific char because of the lambda expression, although you can do so if you change the lambda expression like this: c => c != 't'.






                                                                            share|improve this answer


























                                                                              0












                                                                              0








                                                                              0







                                                                              If you want to remove characters that satisfy a specific condition, you may use this:



                                                                              string s = "SoMEthInG";
                                                                              s = new string(s.ToCharArray().Where(c => char.IsUpper(c)).ToArray());


                                                                              (This will leave only the uppercase characters in the string.)



                                                                              In other words, you may convert the string to an IEnumerable<char>, make changes on it and then convert it back to a string as shown above.



                                                                              Again, this enables to not only remove a specific char because of the lambda expression, although you can do so if you change the lambda expression like this: c => c != 't'.






                                                                              share|improve this answer













                                                                              If you want to remove characters that satisfy a specific condition, you may use this:



                                                                              string s = "SoMEthInG";
                                                                              s = new string(s.ToCharArray().Where(c => char.IsUpper(c)).ToArray());


                                                                              (This will leave only the uppercase characters in the string.)



                                                                              In other words, you may convert the string to an IEnumerable<char>, make changes on it and then convert it back to a string as shown above.



                                                                              Again, this enables to not only remove a specific char because of the lambda expression, although you can do so if you change the lambda expression like this: c => c != 't'.







                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Jun 26 '16 at 16:49









                                                                              florienflorien

                                                                              195312




                                                                              195312























                                                                                  -1














                                                                                  use



                                                                                  myString.Replace ("c", "")





                                                                                  share|improve this answer
























                                                                                  • This is a duplicate of an answer from 2013 by Ian Grainger.

                                                                                    – Joe Gayetty
                                                                                    Apr 17 '18 at 14:34
















                                                                                  -1














                                                                                  use



                                                                                  myString.Replace ("c", "")





                                                                                  share|improve this answer
























                                                                                  • This is a duplicate of an answer from 2013 by Ian Grainger.

                                                                                    – Joe Gayetty
                                                                                    Apr 17 '18 at 14:34














                                                                                  -1












                                                                                  -1








                                                                                  -1







                                                                                  use



                                                                                  myString.Replace ("c", "")





                                                                                  share|improve this answer













                                                                                  use



                                                                                  myString.Replace ("c", "")






                                                                                  share|improve this answer












                                                                                  share|improve this answer



                                                                                  share|improve this answer










                                                                                  answered Jan 26 '16 at 6:30









                                                                                  ChintanChintan

                                                                                  9013




                                                                                  9013













                                                                                  • This is a duplicate of an answer from 2013 by Ian Grainger.

                                                                                    – Joe Gayetty
                                                                                    Apr 17 '18 at 14:34



















                                                                                  • This is a duplicate of an answer from 2013 by Ian Grainger.

                                                                                    – Joe Gayetty
                                                                                    Apr 17 '18 at 14:34

















                                                                                  This is a duplicate of an answer from 2013 by Ian Grainger.

                                                                                  – Joe Gayetty
                                                                                  Apr 17 '18 at 14:34





                                                                                  This is a duplicate of an answer from 2013 by Ian Grainger.

                                                                                  – Joe Gayetty
                                                                                  Apr 17 '18 at 14:34


















                                                                                  draft saved

                                                                                  draft discarded




















































                                                                                  Thanks for contributing an answer to Stack Overflow!


                                                                                  • Please be sure to answer the question. Provide details and share your research!

                                                                                  But avoid



                                                                                  • Asking for help, clarification, or responding to other answers.

                                                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                                                  To learn more, see our tips on writing great answers.




                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function () {
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3670505%2fwhy-is-there-no-char-empty-like-string-empty%23new-answer', 'question_page');
                                                                                  }
                                                                                  );

                                                                                  Post as a guest















                                                                                  Required, but never shown





















































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown

































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown







                                                                                  Popular posts from this blog

                                                                                  Bressuire

                                                                                  Vorschmack

                                                                                  Quarantine