Strings
- بنستخدمها عشان نخزن فيها text
- بياخد 2 Bytes لكل حرف
string greeting = "Hello";
string greeting2 = "Nice to meet you!";
- هي عبارة عن Reference Type
- عبارة عن class وجواه properties and methods
- ال string عبارة عن immutable يعني لما بضيف حاجة أو بشيل حاجة بيعمل string جديدة خالص مختلفة عن القديمة ودا بيأثر على سرعة العملية وعلى الميموري
- الحل هو ال StringBuilder واللي بيفرق عنها في حاجات كتير Cs StringBuilder
- عبارة عن array of characters وطبيعي لازم تبقا معرفها مسبقا هي هتاخد size قد ايه وهنا المشكلة
Why string always reference type?
- في كل لغات البرمجة اللي قابلتنا عرفنا ان ال string هو reference مش value
لاني لما باجي أحجز string مش ببقا عارف انا هحجز قد ايه بالظبط في ال memory انما لو جيت تحجز
int x
هو بيبقا عارف انه هيحجز 4 byte - ولما أجي اعمل allocate بقا هيعرف انا هحجز كام byte بالظبط
string str; //reference
str = "ABC"; // عرف انا هحجز قد ايه
مش هحتاج في ال Csharp اني اعمل new لان اللغة نفسها بتظبط الحاجات دي
String Length
هي عبارة عن property بترجع عدد الحروف اللي بيتكون منها
خد بالك اننا مش بنكتب قوسين فيها
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine(txt.Length); // 26
Other Methods
Formatting
عندنا methods كتير في ال string بس أشهر اتنين هم ()ToUpper() and ToLower
string txt = "Hello World";
Console.WriteLine(txt.ToUpper()); // HELLO WORLD
Console.WriteLine(txt.ToLower()); // hello world
Trim()
: Remove any leading or trailing blank spaces
string val1 = " a";
string val2 = "a ";
Console.WriteLine(val1.Trim() == val2.Trim()); // True
Contains()
, StartsWith()
, EndsWith()
string pangram = "The quick brown fox jumps over the lazy dog.";
Console.WriteLine(pangram.Contains("fox")); //True
Console.WriteLine(pangram.Contains("cow")); // False
Searching
IndexOf('a')
, LastIndexOf("Hello")
بيقبلوا characters أو strings
Substrings
To the end: Substring(startIndex)
With number of characters: Substring(startIndex, length)
Replacing
Replace character: Replace('a', '!')
Replace word: Replace("corn", "popcorn")
Null checking
String.IsNullOrEmpty(str)
or String.IsNullOrWhiteSpace(str)
Splitting
str.Split(' ')
with delimiter.
Converting
We can convert from number to string and from string to number by Explicit Casting.
Or by i.ToString(format);
- حد بالك من حتة ال C0 دي عشان كدا خلى اللي بعد العلامة العشرية 0 رقم ولو عايز أخليه رقم واحد أستخدم C1 وهكذا
Concatenation
بنستخدم علامة +
عشان أدمج اتنين سوا
string firstName = "Pop";
string lastName = "Corn";
string name = firstName + lastName;
Console.WriteLine(name);
ممكن نستخدم برضو string.Concat()
وهي عبارة عن method جوا ال string
string firstName = "Pop";
string lastName = "Corn";
string name = string.Concat(firstName, lastName);
Console.WriteLine(name);
Format the String
ممكن نستخدم String.format
string str = String.Format("Hello World {0}", x);
// You can use it at Output
Console.Write(String.Format("Hello World {0}", x));
We can use also $
string interpolation
string str = $"Hello World {x}";
We can use @
verbatim, to output raw text
Console.WriteLine($@"C:\Output\{projectName}\Data");
Access Strings
ممكن نوصل لأي عنصر في ال string من خلال ال index
string myString = "Hello";
Console.WriteLine(myString[1]); // Outputs "e"
Console.WriteLine(myString[0]); // Outputs "H"
ممكن نستخدم method اسمها IndexOf()
عشان أعرف ال index بتاع أي عنصر في ال string
myString.IndexOf("e"); // 1
فيه method كمان جميلة أوي اسمها SubString()
عشان تجيبلك string من واحدة قديمة من خلال أول index لحد الآخر
// Full name
string name = "John Doe";
// Location of the letter D
int charPos = name.IndexOf("D");
// Get last name
string lastName = name.Substring(charPos);
// Print the result
Console.WriteLine(lastName);
Empty String
في بعض الأحيان ببقا عايز string فاضية عشان أحط فيها القيم اللي هتطلع
في الحالة دي ممكن نستخدم string.Empty
string number = string.Empty;
for (int i = 0; i < 10; i++)
{
numbers = numbers + i.ToString();
}