desislava_topuzakova

Demo String

Mar 26th, 2022
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DemoString
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string name = "Desi"; //Console.Readline()
  10.             Console.WriteLine(name[0]); //първия символ
  11.             Console.WriteLine(name[name.Length - 1]); //последния символ
  12.             Console.WriteLine(name.Length); //брой на символи
  13.             char [] symbols = name.ToCharArray();// ['D', 'e', 's', 'i']
  14.  
  15.             Console.WriteLine("Desi" + "Test"); //"DesiTest"
  16.             Console.WriteLine("Desi" + 23); //"Desi23"
  17.             Console.WriteLine("Desi" + 2.50); //"Desi2.50"
  18.  
  19.             Console.WriteLine(1 + 1 + "Test" + 1 + 2); //"2Test21"
  20.             Console.WriteLine(name.IndexOf('D')); // 0
  21.             Console.WriteLine(name.IndexOf('s')); // 2
  22.             Console.WriteLine(name.IndexOf('e')); // 1
  23.             Console.WriteLine(name.IndexOf('p')); // -1
  24.  
  25.             string sentence = "The cat in under the table";
  26.             string lastWord = sentence.Substring(21); //table
  27.             Console.WriteLine(lastWord);
  28.             string firstWord = sentence.Substring(0, 3); //The
  29.             Console.WriteLine(firstWord);
  30.  
  31.             string[] words = sentence.Split(' '); //["The", "cat", "in", "under", "the", "table"]
  32.             foreach(string word in words) //цикъл за обхождане на колекции (лист и масив)
  33.             {
  34.                 Console.WriteLine(word);
  35.             }
  36.  
  37.             sentence.Replace(' ', '_');
  38.             Console.WriteLine(sentence); //"The_cat_in_under_the_table"
  39.  
  40.             sentence.Remove('e');
  41.             Console.WriteLine(sentence); //"Th_cat_in_undr_th_tabl"
  42.  
  43.             Console.WriteLine(sentence.Contains('u')); //True
  44.             Console.WriteLine(sentence.Contains('z')); //False
  45.             // True -> ако буквата u я има в текста; False -> ако буквата u я няма в тескта
  46.  
  47.             Console.WriteLine(sentence.ToUpper()); //TH_CAT_IN_UNDR_TH_TABL
  48.             Console.WriteLine(sentence.ToLower()); //th_cat_in_undr_th_tabl
  49.  
  50.             Console.WriteLine(sentence.StartsWith("Th")); //True
  51.             Console.WriteLine(sentence.StartsWith("Table")); //False
  52.             //True -> ако текстът започва с th
  53.             //False -> ако текстът НЕ започва с th
  54.  
  55.             Console.WriteLine(sentence.EndsWith("tabl")); //True
  56.             Console.WriteLine(sentence.EndsWith("The")); //False
  57.             //True -> ако текстът завършва с th
  58.             //False -> ако текстът НЕ завършва с th
  59.  
  60.  
  61.  
  62.         }
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment