How would i remove everything before the first (dot) . in a string?
For example:
3042. Item name 3042.
I want to remove 3042.
so that the string becomes
Item name 3042.
To remove everything before the first occurrence of the character '-' in a string, pass the character '-' as a separator in the partition() function. Then assign the part after the separator to the original string variable. It will give an effect that we have deleted everything before the character '-' in a string.
. trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).
Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .
Have a look at String.Substring
and String.IndexOf
methods.
var input = "3042. Item name 3042.";
var output = input.Substring(input.IndexOf(".") + 1).Trim();
Note that it's also safe for inputs not containing the dot.
string str = "3042. Item name 3042.";
str = str.Substring(str.IndexOf('.') + 1);
Use string.Index of to get the position of the first .
and then use string.Substring to get rest of the string.
Just for kicks, a slightly different way of doing things. Removes things up to and including the first dot
var testStr = @"3042. Item name 3042.";
var dotSplit = testStr.Split(new[]{'.'},2);
var results = dotSplit[1];
You want to remove everything before a dot inclusive the dot itself:
String str = "3042. Item name 3042.";
String result = str.Substring(str.IndexOf(".") + 1 ).TrimStart();
String.Substring Method
(Int32)
(note that i've used TrimStart
to remove the empty space left because your question suggests it)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With