Topics Discussed
- Length of string
- String contains a character or not
- String contains a character or not, ignore case
- String contains a substring or not
- String contains a substring or not, ignore case
- String starts with a character or not
- String starts with a substring or not
- String starts with a substring or not, ignore case
- String ends with a character or not
- String ends with a substring or not
- String ends with a substring or not, ignore case
- get a substring from a string
- get a substring from a string from an index position
- remove all from an index position of the string
- remove all from an index position of the string up to a length
- insert a substring into a string
- check if two strings are equal or not
- split a string into an array using a delimiter
- concatenate strings
- join strings
- check whether string is null or empty
- get a repeated character as string
- use of string class various methods
using System;
namespace ConsoleStringFunc
{
class Program
{
static void Main(string[] args)
{
string myemail = "ak.shrishesh@linkedin.com";
Console.WriteLine(myemail.Length); //25
Console.WriteLine("\nContains");
Console.WriteLine(myemail.Contains("@")); //True
Console.WriteLine(myemail.Contains("Link", StringComparison.CurrentCultureIgnoreCase)); //True
Console.WriteLine(myemail.Contains('@')); //True
Console.WriteLine(myemail.Contains('R')); //False
Console.WriteLine(myemail.Contains('R', StringComparison.CurrentCultureIgnoreCase)); //True
Console.WriteLine("\nStartsWith");
Console.WriteLine(myemail.StartsWith('a')); //True
Console.WriteLine(myemail.StartsWith("ak")); //True
Console.WriteLine(myemail.StartsWith("Ak", StringComparison.CurrentCultureIgnoreCase)); //True
Console.WriteLine("\nEndsWith");
Console.WriteLine(myemail.EndsWith('@')); //False
Console.WriteLine(myemail.EndsWith("@")); //False
Console.WriteLine(myemail.EndsWith("com")); //True
Console.WriteLine(myemail.EndsWith("Com", StringComparison.CurrentCultureIgnoreCase)); //True
Console.WriteLine("\nSubstring");
//start position i.e. index starts from 0, length of substring
Console.WriteLine(myemail.Substring(3));//shrishesh@linkedin.com, index=3
Console.WriteLine(myemail.Substring(3, 9));//shrishesh, index=3, length=9
Console.WriteLine("\nRemove(int startIndex, int Count)");
Console.WriteLine(myemail.Remove(3));//ak.
Console.WriteLine(myemail.Remove(3, 9)); //ak.@linkedin.com
Console.WriteLine("\nEquals(string? value, )");
Console.WriteLine(myemail.Equals("ak.shrishesh@linkedin.com")); //True
Console.WriteLine(myemail.Equals("AK.shrishesh@linkedin.com")); //False
Console.WriteLine(myemail.Equals("AK.shrishesh@linkedin.com", StringComparison.CurrentCultureIgnoreCase)); //True
Console.WriteLine(myemail.Insert(3, "Ajeet"));//ak.Ajeetshrishesh@linkedin.com
Console.WriteLine("LastIndexOf has 9 overloads");
/*
* Reports the zero-based index position of the last occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance
*/
Console.WriteLine(myemail.LastIndexOf("@"));//12
Console.WriteLine(myemail.LastIndexOf("@", 15));//12
Console.WriteLine(myemail.LastIndexOf("@", myemail.Length));//12
Console.WriteLine(myemail.LastIndexOf("shri"));//3
Console.WriteLine(myemail.LastIndexOf("shri", 2));//-1
Console.WriteLine("Split() method has 10 overloads and returns array of strings");
string[] texts = myemail.Split("@");
foreach (var text in texts)
{
Console.WriteLine(text);
}
//How many times i is repeated in myemail
int repeatCounts = myemail.Split("x").Length - 1;
Console.WriteLine("i appears {0} times.",repeatCounts);
//repeat a character n times
string rept = new string('A', 5);
Console.WriteLine(rept);//AAAAA
// string class methods
Console.WriteLine(string.Concat("ajeet", "shrishesh", "@","gmail.com"));//ajeetshrishesh@gmail.com
string zls = string.Empty; //zero length string
Console.WriteLine(zls.Length);
Console.WriteLine(string.IsNullOrEmpty(zls)); //True
Console.WriteLine(string.Format("{0:c}", 100));//$100.00 currency format
Console.WriteLine(string.Format("{0:d}", 100));//100 , decimal format
Console.WriteLine(string.Format("{0:e}", 100));//1.000000e+002
Console.WriteLine(string.Format("{0:p}", 100));//10,000.00%
Console.WriteLine(string.Format("{0:x}", 100));//64 hexadecimal format
Console.WriteLine(string.IsNullOrWhiteSpace("Ajeet Kumar"));//False
Console.WriteLine(string.IsNullOrWhiteSpace(" "));//True
Console.WriteLine(string.Join('-', new string[] { "Ajeet", "Kumar", "Shrishesh" }));//Ajeet-Kumar-Shrishesh
string str1 = "Ajeet";
string str2 = "Ajeet";
//ReferenceEquals determines whether the specified System.Object instances are the same instance.
Console.WriteLine(string.ReferenceEquals(str1, str2));//True
string str3 = "ajeet";
Console.WriteLine(string.ReferenceEquals(str1, str3));//False
string obj1 = new string("Ajeet");
string obj2 = new string("Ajeet");
Console.WriteLine(string.ReferenceEquals(obj1, obj2)); //False
Console.Read();
}
}
}
No comments:
Post a Comment