C#. last minute prep Flashcards
(9 cards)
How to convert character/string to upper/lowercase?
string one = “Hi”;
string oneUpper = one.ToUpper();
char two= “h”;
char twoUpper = Char.ToUpper(two);
What does .ToUpper() do for numbers?
It just returns the number on its own, does not produce an errror.
How to check if a character is upper/lowercase, or a digit?
char two= “H”;
Char.IsUpper(two); //true
int three = 9;
Char.IsDigit(three) //true
Math operators and rules for truncating, power, and rounding (to a select D.P.) on int num;
double num = 3.1415;
double power = Math.Pow(2,4);
double truncate = Math.Truncate(num);
double rounded = Math.Round(num, 2);
must always work in doubles.
String handling, how to do:
- Index of a character (and if it isn’t present)
- Last index of a character (and if it isn’t present)
- Make a substring
string EmailPosition = “faisal@lgs.slough.sch.uk”;
int PosOfAtSign = EmailPosition.IndexOf(“@”);
int PosOfL = EmailPosition.LastIndexOf(“l”);
Console.WriteLine(PosOfL);
string hi= “Berub”;
string hi1 = hi.Substring(0, hi.Length);
//(start index, num of characters)
Appending strings
string text = “”;
text += “A”;
text += “3”;
How to convert from CHAR to ASCII code, and ASCII code to CHAR?
ASCII code TO INT:
char ascii = ‘A’;
int number = (int)ascii;
ASCII code to character:
int code = 66;
char ascii = (char)code ;
Convert from F/D to INT, and STRING to F/D
int number = (int)double ; //truncates
double = double.Parse(string);
List functions:
- Convert from string to list of characters
- Insert value at specific index
- Index of a value
- Remove a value
- Remove the item at a specific index
- Sort into alphabetic order A -> Z
- Reserve order of list
List<char> letters = string.ToList();</char>
letters.Insert(3, ‘A’)
letters.IndexOf(‘A’) //first instance, or -1
letters.Remove(‘A’) //remove first instance
letters.RemoveAt(2)
letters.Clear() //empty list
letters.Sort() //alphabetic order
letters.Reverse() //reverses order