oshirowanen
oshirowanen

Reputation: 15965

Replace part of string

I'm having difficulty with the following.

In VB.Net, I have the following line:

Dim intWidgetID As Integer = CType(Replace(strWidget, "portlet_", ""), Integer)

where strWidget = portlet_n

where n can be any whole number, i.e.

portlet_5

I am trying to convert this code to C#, but I keep getting errors, I currently have this:

intTabID = Convert.ToInt32(Strings.Replace(strTabID, "tab_group_", ""));

which I got using an online converter

But it doesn't like Strings

So my question is, how to I replace part of a string, so intTabID becomes 5 based on this example?

I've done a search for this, and found this link: C# Replace part of a string

Can this not be done without regular expressions in c#, so basically, I'm trying to produce code as similar as possible to the original vb.net example code.

Upvotes: 0

Views: 624

Answers (4)

HatSoft
HatSoft

Reputation: 11201

Their is no Strings class in Vb.Net so please use the string class instead http://msdn.microsoft.com/en-us/library/aa903372(v=vs.71).aspx

you can achieve it by this way

string strWidget = "portlet_n";

int intWidgetID  = Convert.ToInt32(strWidget.Split('_')[1]);

Upvotes: 0

JohnnBlade
JohnnBlade

Reputation: 4327

It should be like this strTabID.Replace("tab_group_", string.Empty);

    int intTabID = 0;
    string value = strTabID.Replace("tab_group_", string.Empty);
    int.TryParse(value, out intTabID);

    if (intTabID > 0)
    { 

    }

And in your code i think you need to replace "tab_group_" with "portlet_"

Upvotes: 2

V4Vendetta
V4Vendetta

Reputation: 38230

This should work

int intWidgetID = int.Parse(strTabID.Replace("tab_group_",""));//Can also use TryParse

Upvotes: 0

Dan Puzey
Dan Puzey

Reputation: 34218

Instead of Strings.Replace(strTabID, "tab_group_", ""), use strTabID.Replace("tab_group_", "").

Upvotes: 0

Related Questions