Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove everything before the first dot in string?

Tags:

c#

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.
like image 511
Elvin Avatar asked Oct 19 '12 07:10

Elvin


People also ask

How to remove everything before a string Python?

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.

How do you delete everything before a character in a string Java?

. 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).

How do you remove everything from a string after a character?

Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .


4 Answers

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.

like image 93
Michal Klouda Avatar answered Oct 14 '22 04:10

Michal Klouda


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.

like image 25
Habib Avatar answered Oct 14 '22 04:10

Habib


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];
like image 30
MarcE Avatar answered Oct 14 '22 06:10

MarcE


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)

like image 31
Tim Schmelter Avatar answered Oct 14 '22 05:10

Tim Schmelter