String Manipulations Technique in C#
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
- ToUpper
- ToLower
- Using Text Info Dictionary to ToTitleCase
Out Put: -
String To Array : -
Out Put :-
Escape Sequence : -
Out Put : -
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
- Concat :- Convert Array To String
- Join :- Convert Array to String but with Some Special Parameters between them.
- Split :- Convert String to Array with Character between Them
Practical Implementation : -
Out Put :-
Pad And Trim Function :-
Out Put :-
Searching From String : -
- StartsWith : -Match The Exact Words from String in Start and Return Bool
- EndsWith :- Match The Exact Words from String in End and Return Bool
- IndexOf :- Return Integer of the Word Present in String by Reading Matching String Index from the start of the string.
- 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
- Compare To : — Which string needs to be Compared can only be null(Object)
- Compare : — Both string can be null
- Equality:- You can Allow for Not Case Sensitive
- == : — You cannot Allow for Not Case Sensitive
Compare To Implementation : -
CompareToHelper(“A”, “B”);
Will Check for Alphabetical Order Ordering
Gives Integer Output
- -1 if b is greater than a
- 1 if a is greater than b
- 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:-