Strings Flashcards

1
Q

Check if string s is null or empty.

A

string.IsNullOrEmpty(s);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Class that represents a mutable string of characters.

A

StringBuilder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Convert string s into char array.

A

s.ToCharArray();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Length of a string s.

A

s.Length;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Swap t[0] and t[1] with tuple deconstruction and assignment.

A

(t[0],t[1])=(t[1],t[0]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Create string from table of chars named characters.

A

new string(characters);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Reverse array tab in one line.

A

Array.Reverse(tab);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will happen in this code (the method has parameter of type string):
SomeGetByStringMethod(null);

A

No error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will happen in this code:
object obj = null;
SomeGetByStringMethod(obj);

A

Compilation error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will happen in this code:
typeof(Program).GetMethod(“SomeGetByStringMethod”).Invoke(null, new object[] { 123 });

A

Runtime error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will happen in this code:
typeof(Program).GetMethod(“TEST”).Invoke(null, new object[] { “abc” });

A

No error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will happen in this code (the method has parameter of type string):
SomeGetByStringMethod(123);

A

Compilation error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create “Hello, <name>!" string with string interpolation, where name is the variable.</name>

A

$”Hello, {name}”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Check if string s is null or white space.

A

string.IsNullOrWhiteSpace(s);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Compare strings a and b case sensitive.

A

a == b;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Compare strings a and b case insensitive.

A

string.Equals(a,b,StringComparison.OrdinalIgnoreCase);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the comparison method that will return numerical value based on strings a and b?

A

string.Compare(a,b,StringComparison.OrdinalIgnoreCase);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to check if
string sentence = “The quick brown fox”;
has a word “brown” in it?

A

sentence.Contains(“brown”);

19
Q

How to check if
string sentence = “The quick brown fox”;
has a word “The” at the beginning?

A

sentence.StartsWith(“The”);

20
Q

How to check if
string sentence = “The quick brown fox”;
has a word “fox” at the end?

A

sentence.EndsWith(“fox”);

21
Q

How to check in
string sentence = “The quick brown fox”;
the index that word “quick” starts in?

A

sentence.IndexOf(“quick”);

22
Q

How to check in
string sentence = “The quick brown fox”;
the last index of a letter ‘o’?

A

sentence.LastIndexOf(“o”);

23
Q

How to retrieve word “quick” from the sentence “The quick brown fox”?

A

sentence.Substring(4,5);

24
Q

How to replace word “fox” with “dog” in the sentence “The quick brown fox”?

A

sentence.Replace(“fox”,”dog”);

25
How to add word "very" to the sentence "The quick brown fox"?
sentence.Insert(4,"very");
26
How to remove word "quick" from the sentence "The quick brown fox"?
sentence.Remove(4,6);
27
How to get rid of white spaces in string s = " Hello "; ?
s.Trim();
28
How to get rid of white spaces at the beginning of string s = " Hello "; ?
s.TrimStart();
29
How to get rid of white spaces at the end of string s = " Hello "; ?
s.TrimEnd();
30
How to get fruits from this string s="apple,banana,cherry";?
string[] fruits=s.Split(',');
31
How to get this table elements: string[] s = ["apple","banana","cherry"]; into "apple - banana - cherry"?
string.Join(" - ",s);
32
How to make all characters in string s="hello"; big?
s.ToUpper();
33
How to make all characters in string s="Hello"; small?
s.ToLower();
34
How to add age value into this sentence "The age is ..." with standard formatting?
string.Format("The age is {0}",age);
35
How to add a word "world" into below sb? StringBuilder sb = new StringBuilder("Hello");
sb.Append(" world");
36
How to add a comma into below sb? StringBuilder sb = new StringBuilder("Hello world");
sb.Insert(5,",");
37
How to replace "world" with "C#" in below sb? StringBuilder sb = new StringBuilder("Hello world");
sb.Replace("world","C#");
38
How to create string from below sb? StringBuilder sb = new StringBuilder("Hello world");
sb.ToString();
39
How to convert string 123 to number?
int.Parse("123");
40
How to convert string 123 to a number safe? Save it as newNb.
int.TryParse("123",out newNb);
41
Convert number a=123 to a string.
a.ToString();
42
Convert below date to string in EU format: DateTime date = DateTime.Now;
date.ToString("yyyy-MM-dd")
43
Convert 2024-02-17 string to date.
DateTime.Parse("2024-02-17");
44
Check string s for null and return "default" if it is null.
return input ?? "default;