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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
For those of us that don't program in C#, can you explain what this is supposed to do?
 

krish

Board Regular
Joined
May 17, 2005
Messages
211
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.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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?
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,887
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
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.
 

krish

Board Regular
Joined
May 17, 2005
Messages
211
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;
 

Forum statistics

Threads
1,181,055
Messages
5,927,861
Members
436,573
Latest member
CMR237

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
Top