String Manipulations Technique in C#

Asad Iftikhar
5 min readJun 26, 2021

There are many string Build in functions Available in . Net Frame Work.

This articles almost covers all the Build in Manipulations for String Am Using . Net Frame Work 5.0

String Comparison : -

There are mainly two methods available

  1. ToUpper
  2. ToLower
  3. Using Text Info Dictionary to ToTitleCase

Out Put: -

String To Array : -

Out Put :-

Escape Sequence : -

Out Put : -

s

String Append : -

Out Put:-

String Interpolation And Literal : -

Out Put : -

Difference Between String And String Builder

string is Immutable basically when you Declared a Variable in string it will takes some memory when you Changes its Values its not Write on that Same Memory it create new stack memory address and stores its address in heap.

Where as String Builder takes memory One time and when you Overwrite or Append the string it will takes that same memory Block to Manipulate the String

Here is a Demo difference between String and String Builder by Performance Wise

Out Put :-

Working With Arrays :-

There are three function

  1. Concat :- Convert Array To String
  2. Join :- Convert Array to String but with Some Special Parameters between them.
  3. Split :- Convert String to Array with Character between Them

Practical Implementation : -

Out Put :-

Pad And Trim Function :-

Out Put :-

Searching From String : -

  1. StartsWith : -Match The Exact Words from String in Start and Return Bool
  2. EndsWith :- Match The Exact Words from String in End and Return Bool
  3. IndexOf :- Return Integer of the Word Present in String by Reading Matching String Index from the start of the string.
  4. LastIndexOf :- Return Integer of the Word Present in String by Reading Matching String Index from the end of the string.

Index Function Return Integer -1 if string is not Present

Practical Implementation : -

Out Put :-

String Comparison : -

There are three four types of String Comparison four of them are same with Minor Differences

  1. Compare To : — Which string needs to be Compared can only be null(Object)
  2. Compare : — Both string can be null
  3. Equality:- You can Allow for Not Case Sensitive
  4. == : — You cannot Allow for Not Case Sensitive

Compare To Implementation : -

CompareToHelper(“A”, “B”);

Will Check for Alphabetical Order Ordering

Gives Integer Output

  1. -1 if b is greater than a
  2. 1 if a is greater than b
  3. if a is equal to b

For Enable Nullable String Open Your Project .csProj File and Add these lines

<Nullable>enable</Nullable>```

Practical Implementation : -

Output:-

Compare :-

Compare is same as Compare To but in this case both are Object so both are strings can be null

Practical Implementation : -

Output :-

Equality : -(Can Overpass Lower Case Check )

Output:-

Overpassing Lower Case Check Using Equality:-

Output:-

Using ==

Output :-

Sub String :-

Substring is used for getting string from string

You Pass index of string from where you want to fetch string

Its accepts one or two parameters

Output : -

Replace :-

As C# is Case Sensitive so we need to over pass Case Sensitive by passing Third Parameters as

StringComparison.CurrentCultureIgnoreCase

Practical Implementation : -

Output : -

Remove :-

Output :-

Insert : -

Output:-

--

--