how to conver the C# code into VBA?

krish

Board Regular
Joined
May 17, 2005
Messages
211
Hi,

Could someone help me to convert the following code in to VBa? Appreciate your help. thanks.

Rich (BB code):
private static string ampersandReplacement = "&#" + ((int)'&') + ";";
        private static string singleQuoteReplacement = "&#" + ((int)'\'') + ";";
        private static string doubleQuoteReplacement = "&#" + ((int)'"') + ";";
        private static string lessThanReplacement = "&#" + ((int)'<') + ";";
        private static string greaterThanReplacement = "&#" + ((int)'>') + ";";


 public static String substituteString(String originalStr)
        {
            

            // string substituteString(stringtring originalStr)
            
            if (originalStr == null || "".Equals(originalStr))
                return "";

            StringBuilder replacedSB = new StringBuilder();

            for (int i = 0; i < originalStr.Length; i++)
            {
                char currChar = originalStr;
                switch (currChar)
                {
                    case '&':
                        replacedSB.Append(ampersandReplacement);
                        break;
                    case '\'':
                        replacedSB.Append(singleQuoteReplacement);
                        break;
                    case '"':
                        replacedSB.Append(doubleQuoteReplacement);
                        break;
                    case '<':
                        replacedSB.Append(lessThanReplacement);
                        break;
                    case '>':
                        replacedSB.Append(greaterThanReplacement);
                        break;
                    default:
                        replacedSB.Append(currChar);
                        break;
                }
            }

            return replacedSB.ToString();
        }


EDIT: added code tags - Moderator
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
For those of us that don't program in C#, can you explain what this is supposed to do?
 
Upvote 0
this program replace special characters from a string with &# integer value of the special chr + ;.

if the special chr's are not replaced correctly, the xml parser will throw an exception.
 
Upvote 0
OK Since the ASCII code of a single quote is 39, you want this to be replaced with the text &39 + ;

Is this correct? Including the spaces between the code the + and the semicolon. Is the period the end of your sentence or do you want a period included too?
 
Upvote 0
Hi krish

Try:
Code:
Function substituteString(ByVal sStr As String) As String
Dim sSpecialChars As String, i As Integer

sSpecialChars = "&'""<>"

For i = 1 To Len(sSpecialChars)
    sStr = Replace(sStr, Mid(sSpecialChars, i, 1), "&#" & Asc(Mid(sSpecialChars, i, 1)) & ";")
Next i
substituteString = sStr
End Function

hope this helps
PGC
 
Upvote 0
Krish

I really think we need some more information.

I've not programmed in C# or C++ or any of the related languages for some time but there seems to be something missing from what you posted.
 
Upvote 0
hi,

If have a xml <name = "vvv & xxx">, the xml parser will throw an exception. If I replace the string with,

<name = "vvv 'xxx"> , will work fine. So I am trying to replace the special chr's with &#intvalue of specialchr;
 
Upvote 0

Forum statistics

Threads
1,222,018
Messages
6,163,429
Members
451,837
Latest member
gmc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top