Advertisement
Guest User

useful nippets C# for beginners

a guest
Oct 8th, 2015
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. how to comment code
  2. -----
  3. comment = Ctrl+K, Ctrl+C
  4. uncomment = Ctrl+K, Ctrl+U
  5. ===============================================
  6. how to convert to decimal with .00
  7. -----
  8. Console.WriteLine("{0:0.00 leva}", demo); //this
  9. Console.WriteLine("{0:F2} levs", demo); // or that
  10. ===============================================
  11. how to split a string into list of nums/words
  12. ----
  13. string input = Console.ReadLine();
  14. List<string> listIt = input.Split(' ').ToList();
  15. =================================================
  16. CREATE NEW STRING FOR THE LOOP (Problem number 4 from
  17.  
  18. exams)
  19. ----
  20. string stars = new string('*', 10);
  21. =================================================
  22. split string into characters
  23. ----
  24. char[] characters = "this is a test".ToCharArray();
  25. =================================================
  26. how to remove white space and lower all letters
  27. ----
  28. string newText = text.ToLower().Replace(" ", string.Empty);
  29. =================================================
  30. how to split number into individual digits
  31. ----
  32. string inputLine = Console.ReadLine();
  33. inputLine = inputLine + " " + int.MaxValue;
  34. string[] inputTokens = inputLine.Split(' ');
  35.    for (int i = 0; i < inputTokens.Length; i++)
  36.             {
  37.                 int num = int.Parse(inputTokens[i]);
  38.             }
  39. =================================================
  40. sum the number for example 1254 (working with 4 digits)
  41. ----
  42. int n = int.Parse(Console.ReadLine());
  43. int nSum = 0;
  44. while (n >; 0)
  45. {
  46.     nSum += n % 10;
  47.     n /= 10;
  48. }
  49. =================================================
  50. how to compare time in c#
  51. ----
  52. TimeSpan time = TimeSpan.Parse(Console.ReadLine());
  53. TimeSpan start = new TimeSpan(14, 0, 0);
  54. TimeSpan end = new TimeSpan(18, 59, 0);
  55. if ((time > start) && (time < end))
  56. {
  57.      Console.WriteLine("match found");
  58. }
  59. =================================================
  60. how to create new empty string and append char/s to it
  61. ----
  62. var string = new StringBuilder();
  63. string.Append(character[i]);
  64. =================================================
  65. convert StringBuilder to String
  66. ----
  67. string newString = oldString.ToString();
  68. =================================================
  69. subtract string with the function
  70. ----
  71. string leftFirstStr = "test".Substring(0, firstIndex);
  72.  
  73. //outputs: "est"
  74. =================================================
  75. how to convert char into ASCII (int)
  76. ----
  77. int ascii = (int)Convert.ToChar(item)
  78. =================================================
  79. how to convert int into hex
  80. ----
  81. int.ToString("X"); //IMPORTANT "X" HAVE TO STAY, DONT
  82.  
  83. CHANGE IT
  84. =================================================
  85. find all possible combinations of a string
  86. ----
  87. string alphabet = Console.ReadLine();
  88. int n = int.Parse(Console.ReadLine());
  89.  
  90. var q = alphabet.Select(x =>; x.ToString());
  91. int size = 5; //enter the length of the string...
  92. for (int i = 0; i < size - 1; i++)
  93. {
  94.     q = q.SelectMany(x =>; alphabet, (x, y) =>; x + y);
  95. }
  96. foreach (var item in q)
  97. {
  98.     Console.WriteLine(item);
  99. }
  100. ============================================
  101. Create new list
  102. -----
  103. List<int> decimalArray = new List<int>();
  104. ============================================
  105.  
  106. TIPS:
  107. using arrays/string in a loop = constant.Length
  108. using list in a loop = constant.Count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement