How to check if string is null before replacing charecters in C-Sharp (C#)

Question: How do you catch for nulls in a variable before calling String.Replace()

Answer: If you have a variable that might be null at some point, just add StringVariable?.Replace(" ",","); What this line of code does is, It first looks into a String Variable called StringVariable and checks if it is null, if it is not null then the code goes ahead to replace the "Space" with a comma character. You might have seen a situation where an integer placed as a parameter has a question mark in front of it,  this handles the same scenario where the Integer might be null (not passed in with a value).

Edit this Article
String.Replace() Technology published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy