Elvin
Elvin

Reputation: 367

Remove everything before the first dot in string?

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.

Upvotes: 6

Views: 8035

Answers (4)

MarcE
MarcE

Reputation: 3731

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];

Upvotes: 2

Habib
Habib

Reputation: 223362

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.

Upvotes: 4

Michal Klouda
Michal Klouda

Reputation: 14521

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.

Upvotes: 14

Tim Schmelter
Tim Schmelter

Reputation: 460238

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)

Upvotes: 3

Related Questions