Replace Mutiple Asp Classoc
Just thought I'd revive an old, but useful thread. Found this post after searching around for a solution to validating a user-entered filename.
The replace method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. Note: If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified value, use the global (g.
Here it is in VB.NET:)Imports System.Text.RegularExpressions Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Label1 AsSystem.Web.UI.WebControls.Label #Region ' Web Form Designer Generated Code ' 'This call is required by the Web Form Designer. Private Sub InitializeComponent End Sub Private Sub PageInit(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent End Sub #End Region Private Sub PageLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadEnd Sub Private Sub Button1Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim SourceString As String = TextBox1.Text Dim rgPattern = '/:.?' ' ' Dim objRegEx As New Regex(rgPattern) Label1.Text = 'New Name: ' & objRegEx.Replace(SourceString,') End Sub End Class - Bryan.
May 4th, 2007Hey, Scripting Guy! I saw your column on, and that’s almost the solution I’m looking for. However, the text I need to replace is the single quote mark, and I can’t figure out how to do that. How do I replace single quote marks in a text file?— JBHey, JB. You know, this is shaping up to be another very hectic day for the Scripting Guys; after all, we have meetings in the morning as well as meetings in the afternoon. That would be bad enough, but these meetings also involve such heavy-duty issues as our budget for the coming year and the new system that we will eventually have to use in order to manage the Script Center.
And yet, as busy as he is, there’s still one thing that remains first and foremost in the mind of the Scripting Guy who writes this column: what should he have for dinner tonight?Maybe chicken casserole; the Scripting Family hasn’t had chicken casserole in awhile. And maybe “candied carrots,” you know, the ones with the butter and the brown sugar glaze on them. Or maybe.To be honest, JB, this could take awhile; after all, important decisions like this should never be made lightly. While you wait, feel free to read a magazine. If you don’t happen to have a magazine handy, then feel free to read the following script, one that replaces all the single quote marks in a text file. In fact, not only does it replace the single quote marks, but it replaces them with double quote marks!OK, sure, that sounds impossible.
In a typical search-and-replace script this would be the point where we would call the VBScript Replace function and replace the single quote marks with double quote marks. We’re guessing that JB has tried to do just that, using a line of code similar to this: strNewText = Replace(strText, “‘”, “””)Now, syntactically, that looks OK; after all, we’ve called the Replace function followed by three parameters:.strText, the text we want to do the search-and-replace operation on.“‘”, the single quote mark (embedded between a pair of double quote marks), representing the text to be replaced.“””, the double quote mark (embedded between a pair of double quote marks), representing the replacement text.Like we said, that looks OK. But here’s what happens when we try to use this code: C:Scriptstest.vbs(13, 40) Microsoft VBScript compilation error: Unterminated string constantYikes!
What’s that all about?As you might have guessed, the problem is due to the fact that both the single quote mark and the double quote mark have special meanings in VBScript. The single quote mark, to name one use, indicates a comment added to a script: ‘ This is a commentMeanwhile, the double quote mark is used – among other things – to enclose string values: x = “This is a string value.”Because these characters have special meanings, all we’ve managed to do so far is to confuse VBScript. Free mp3 hindi songs mp3 jio. For example, a human being might look at three consecutive double quote marks and think, “Oh, this is just a double quote mark surrounded by two other double quote marks.” Unfortunately, though, VBScript doesn’t see it that way; instead, VBScript desperately tries to interpret that statement using its built-in parsing rules. One of those rules says that, for every opening double quote mark, there must be a corresponding ending quote; in other words, double quote marks must travel in pairs.
When VBScript sees an odd number (three) of double quote marks it assumes that one of the pairs is missing a quote mark.Is that a problem? You bet it is: because of the syntax error the script won’t even compile, let alone run.This – or some variation of it – is the situation JB has run into.
So how do we work around the issue? There are actually a couple different ways to do that, but we chose this approach: strOldText = Chr(39)strNewText = Chr(34)What we’re doing here is using the Chr function to assign values to a couple of variables (strOldText and strNewText). If you provide Chr with an integer value (such as 39) it will report back the character that corresponds to that value.
Guess what character has an ASCII value of 39? You got it: the single quote mark. As for ASCII value 34, that – well, needless to say, you’re way ahead of us: yes, Chr(34) is the double quote mark.See what we’re doing here? In the first line we’ve assigned the single quote mark to the variable strOldText; this enables us to use a reference to the single quote mark without using the character itself. We then do the same thing with the double quote mark. Because now we can call the Replace function and use variable names rather than problematic statements like “””: strNewText = Replace(strText, strOldText, strNewText)This statement is syntactically correct.
Replace Multiple Asp Classic Server
Equally important, it will replace all the single quote marks with double quote marks.Note. So how did we know that Chr(39) was the single quote mark and Chr(34) was the double quote mark? Believe it or not that was something we just plain knew.
(Sometimes the Scripting Guys do know what they’re talking about sort of) But you don’t have to know that Chr(39) is the single quote mark; instead you can get ASCII values any time you need them simply by looking in the. The rest of the script practically writes itself. After reopening Test.txt (this time for writing) we call the Write method and replace the existing contents of the file with our newly-revised contents (the ones stored in the variable strNewText). We then close the text file and go back to planning tonight’s dinner. Does garlic bread sound good to anyone else?Incidentally, when all is said and done our text file will look like this: “Ken Myer”,”Finance”,”Senior Analyst”“Pilar Ackerman”,”Human Resources”,”Manager”“Jonathan Haas”,”Transportation”,”Director”Which is exactly the way we want it to look.And now that today’s column is done, and dinner is planned, it’s time to head off to our first meeting, the one about budgets. Last year, for the first time ever, the Scripting Guys actually got a budget; we took that money and immediately spent the whole thing on.
Classic Asp Multiple Replace
That should make this year’s budget meeting an interesting one, to say the least.