Strings Flashcards
Check if string s is null or empty.
string.IsNullOrEmpty(s);
Class that represents a mutable string of characters.
StringBuilder
Convert string s into char array.
s.ToCharArray();
Length of a string s.
s.Length;
Swap t[0] and t[1] with tuple deconstruction and assignment.
(t[0],t[1])=(t[1],t[0]);
Create string from table of chars named characters.
new string(characters);
Reverse array tab in one line.
Array.Reverse(tab);
What will happen in this code (the method has parameter of type string):
SomeGetByStringMethod(null);
No error.
What will happen in this code:
object obj = null;
SomeGetByStringMethod(obj);
Compilation error.
What will happen in this code:
typeof(Program).GetMethod(“SomeGetByStringMethod”).Invoke(null, new object[] { 123 });
Runtime error.
What will happen in this code:
typeof(Program).GetMethod(“TEST”).Invoke(null, new object[] { “abc” });
No error.
What will happen in this code (the method has parameter of type string):
SomeGetByStringMethod(123);
Compilation error.
Create “Hello, <name>!" string with string interpolation, where name is the variable.</name>
$”Hello, {name}”;
Check if string s is null or white space.
string.IsNullOrWhiteSpace(s);
Compare strings a and b case sensitive.
a == b;
Compare strings a and b case insensitive.
string.Equals(a,b,StringComparison.OrdinalIgnoreCase);
What is the comparison method that will return numerical value based on strings a and b?
string.Compare(a,b,StringComparison.OrdinalIgnoreCase);
How to check if
string sentence = “The quick brown fox”;
has a word “brown” in it?
sentence.Contains(“brown”);
How to check if
string sentence = “The quick brown fox”;
has a word “The” at the beginning?
sentence.StartsWith(“The”);
How to check if
string sentence = “The quick brown fox”;
has a word “fox” at the end?
sentence.EndsWith(“fox”);
How to check in
string sentence = “The quick brown fox”;
the index that word “quick” starts in?
sentence.IndexOf(“quick”);
How to check in
string sentence = “The quick brown fox”;
the last index of a letter ‘o’?
sentence.LastIndexOf(“o”);
How to retrieve word “quick” from the sentence “The quick brown fox”?
sentence.Substring(4,5);
How to replace word “fox” with “dog” in the sentence “The quick brown fox”?
sentence.Replace(“fox”,”dog”);