How can I check whether a string variable is empty or null in C#? [duplicate]
if (string.IsNullOrEmpty(myString)) { // }
if (string.IsNullOrEmpty(myString)) { // }
Well, if the string really ends with the pattern, you could do this: str = str.replace(new RegExp(list[i] + ‘$’), ‘finish’);
As groovy doesn’t have EOL marker (such as 😉 it gets confused if you put the operator on the following line This would work instead: def a = “test” + “test” + “test” as the Groovy parser knows to expect something on the following line Groovy sees your original def as three separate statements. The … Read more
You can use Enumerable.Take like: char[] array = yourStringVariable.Take(5).ToArray(); Or you can use String.Substring. string str = yourStringVariable.Substring(0,5); Remember that String.Substring could throw an exception in case of string’s length less than the characters required. If you want to get the result back in string then you can use: Using String Constructor and LINQ’s Take … Read more
Here’s a neat solution: String upToNCharacters = s.substring(0, Math.min(s.length(), n)); Opinion: while this solution is “neat”, I think it is actually less readable than a solution that uses if / else in the obvious way. If the reader hasn’t seen this trick, he/she has to think harder to understand the code. IMO, the code’s meaning … Read more
The second option really isn’t the same as the others – if the string is “///foo” it will become “foo” instead of “//foo”. The first option needs a bit more work to understand than the third – I would view the Substring option as the most common and readable. (Obviously each of them as an … Read more
There’s one string in the intern pool, which will be reused every time you run the code. Then there’s the extra string which is constructed each time you run that line. So for example: for (int i = 0; i < 10; i++) { String s = new String(“abcd”); } will end up with 11 … Read more
Although: max(len(w) for w in words) does kind of “read” easier – you’ve got the overhead of a generator. While: len(max(words, key=len)) can optimise away with the key using builtins and since len is normally a very efficient op for strings, is going to be faster…
As your string currently stands, the word لطيفة is stored prior to the word اليوم; the fact that اليوم is displayed “first” (that is, further to the left), is just a (correct) result of the Unicode Bidirectional Algorithm in displaying the text. That is: the string you start with (“Test:لطيفة;اليوم;a;b”) is the result of the … Read more
What you are trying to do is get all the permutations of a collection. Unique permutations of list permutations of k objects from a set of n algorithm Here is the code snippet: static void Main(string[] args) { var list = new List<string> { “a”, “b”, “c”, “d”, “e” }; var result = GetPermutations(list, 3); … Read more