How can I do take a substring and length of a string in C# if that string might be a null? -


i have following strings:

var temp = null; var temp = ""; var temp = "12345678"; var temp = "1234567890"; 

what need if have function give me last 4 digits of input variable if input variable 8 or 10 characters long. otherwise need return "";

is there easy way can in c#. not sure how deal null because if length of null think give me error.

int length = (temp ?? "").length; string substring = ""; if(length == 8 || length == 10) { substring = temp.substring(length - 4); } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -