Textbook The Last Operators of String Type: Null or Empty Method

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Textbook

Chapter 14
The last operators of String type

Null or empty method


{
string str;
Console.WriteLine("Enter sentence whatever you want");
str = Convert.ToString(Console.ReadLine());
if (string.IsNullOrEmpty(str))
{
Console.WriteLine("Please write a sentence");
}
else
{
Console.WriteLine(str);
}
Console.ReadKey();
}

Comparison operator
{
string a="ZU-047";
string b="ZU-047";
//Console.WriteLine(a==b);
Console.WriteLine(a.Equals(b));
Console.WriteLine(a.Equals("047"));
Console.ReadKey();
}

Result is

True

False

Write a program that copy to first 10 symbols from the text


{
string str;
char[] ch = new char[100];
Console.WriteLine("Enter sentence whatever you want");
str = Convert.ToString(Console.ReadLine());
str.CopyTo(0, ch, 0, 10);
Console.WriteLine(ch);
Console.ReadKey();
}
Write a program that deletes all symbols till “computer” word.
{
string str;
Console.WriteLine("Enter sentence whatever you want");
str = Convert.ToString(Console.ReadLine());
int index = str.IndexOf("computer");
Console.WriteLine(str.Remove(0,index));
Console.ReadKey();
}

Write a program that deletes all symbols after “computer” word.


{
string str;
Console.WriteLine("Enter sentence whatever you want");
str = Convert.ToString(Console.ReadLine());
int index = str.IndexOf("computer");
Console.WriteLine(str.Remove(index+8,str.Length-index-8));
Console.ReadKey();
}

If there is no any “computer” word in your entered sentence, then for conclusion index will be
equal to “-1” and because of this line

***Console.WriteLine(str.Remove(index+8,str.Length-index-8));***

Index+8 will be “-1+8=7”. So from 7th symbol our all symbols will erase from the text.

Write a program that removes all symbols before and after “computer” word.
{
string str;
Console.WriteLine("Enter sentence whatever you want");
str = Convert.ToString(Console.ReadLine());
int index = str.IndexOf("computer");
str = str.Remove(index + 8, str.Length - index - 8);
str = str.Remove(0, index);
Console.WriteLine(str);
Console.ReadKey();
}

You might also like