`Trim`, `TrimStart`, and `TrimEnd`
In C#, the Trim
,
TrimStart
, and TrimEnd
methods are used to
remove whitespace or specific characters from the beginning, end, or both ends
of a string. Here's an overview of each method with definitions and examples:
TrimStart
- Purpose: Removes all
leading whitespace characters from the string, or a set of specified
characters.
- Be default it
removes all leading white spaces If
You are not provides any character
String str=” *#^ Hello Software Engineers ^#* ”
string result =
str.TrimStart();
output = ”*#^ Hello Software Engineers ^#* ”
Note : if nothing provided leading spaces get trimmed
String str=” *#^ Hello Software Engineers ^#* ”
string result =
str.TrimStart(‘*’);
output : ” *#^
Hello Software Engineers ^#* ”
Note : here leading char is space that’s why no trim happen
String str=” *#^ Hello Software Engineers ^#* ”
string result =
str.TrimStart(‘ ’,‘*’,’#’,’^’);
output : “Hello Software Engineers ^#* ”
Note : here leading char (‘ ’,‘*’,’#’,’^’) leading chars are removed ,
order of params not required
TrimEnd
- Purpose: Removes all
trailing whitespace characters from the string, or a set of specified
characters.
- Be default it
removes all ending white spaces If You
are not provides any character
String str=” *#^ Hello Software Engineers ^#* ”
string result = str.TrimEnd();
output = ” *#^
Hello Software Engineers ^#* ”
Note : if nothing provided ending spaces get trimmed
String str=” *#^ Hello Software Engineers ^#* ”
string result = str. TrimEnd(‘*’);
output : ” *#^
Hello Software Engineers ^#* ”
Note : here End char is space that’s why no trim happen
String str=” *#^ Hello Software Engineers ^#* ”
string result = str. TrimEnd(‘
’,‘*’,’#’,’^’);
output : “ *#^ Hello
Software Engineers ”
Note : here leading char (‘ ’,‘*’,’#’,’^’) leading chars are
removed , order of params not required
Trim
- Purpose: Removes all
leading and trailing whitespace characters from the string, or a set of
specified characters.
- All leading and
ending spaces get trimmed if nothing provided
String str=” *#^ Hello Software Engineers ^#* ”
string result = str.Trim();
output = ”*#^ Hello Software Engineers ^#* ”
Note : if nothing provided ending spaces get trimmed
String str=” *#^ Hello Software Engineers ^#* ”
string result = str. Trim(‘*’);
output : ” *#^
Hello Software Engineers ^#* ”
Note : here leading & End char is space that’s why no
trim happen
String str=” *#^ Hello Software Engineers ^#* ”
string result = str. Trim(‘ ’,‘*’,’#’,’^’);
output : “ Hello Software Engineers ”
Note : here leading & ending char (‘ ’,‘*’,’#’,’^’) are
removed , order of params not required
String str=” *#^ Hello ^#* ” |
|||
Parameters |
Trim |
TrimStart |
TrimEnd |
None |
”*#^ Hello ^#*” |
”*#^ Hello ^#* ” |
” *#^ Hello ^#*” |
(‘*’) |
” *#^ Hello ^#* ” |
” *#^ Hello ^#* ” |
” *#^ Hello ^#* ” |
(‘ ’,‘*’,’#’,’^’); |
“Hello” |
”Hello ^#* ” |
” *#^ Hello” |
Key Points
- Whitespace
Removal: By default, all three methods remove whitespace characters
(spaces, tabs, newlines, etc.).
- Character
Removal: You can specify an array of characters to be removed by passing
them as arguments to these methods.
- Usage:
- Use Trim when you need
to remove characters from both the beginning and end of a string.
- Use TrimStart when you need
to remove characters only from the beginning of a string.
- Use TrimEnd when you need
to remove characters only from the end of a string.
No comments:
Post a Comment
Thanks for the contribution, our team will check and reply back if response required.