0

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.

4 Answers 4

2

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_"

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I'm now getting the following error message Compiler Error Message: CS1501: No overload for method 'Replace' takes '3' arguments.
Yes, sorry, I was too quick with my answer: you're using strings instead of string, but also the method is different: it's an instance method. See my edit.
0

This should work

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

Comments

0

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

2 Comments

I'm now getting the following error message Compiler Error Message: CS1501: No overload for method 'Replace' takes '3' arguments.
@oshirowanen please see another way of achieving it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.