469,917 Members | 1,644 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

Tricky Replace String Question


I have a string that looks like this:

"document.form1.textBox1.focus
();document.form1.textBox1.select();"
I want to replace the text between "document.form1."
and ".focus()",
as well as the text betwen "document.form1." and ".select
()".

Ultimately the effect is replacing "textBox1"
with "textBox2",
but my method has no way of knowing that "textBox1" is
the
text that must be replaced, since the entire string comes
to it.

How do I replace a value when I don't know what the value
is?

I don't think I want to use a regex because of the speed
penalty in instancing the class over and over.

Thanks.

Nov 18 '05 #1
6 1311
regex is your best bet, the replace property is static so you don't have to
instance the class. have a look at regexlib.com

--
Regards,
Alvin Bruney
Got DotNet? Get it here...
http://www.networkip.net/dotnet/tidbits/default.htm
"localhost" <pr*******@cohort.ces> wrote in message
news:03****************************@phx.gbl...

I have a string that looks like this:

"document.form1.textBox1.focus
();document.form1.textBox1.select();"
I want to replace the text between "document.form1."
and ".focus()",
as well as the text betwen "document.form1." and ".select
()".

Ultimately the effect is replacing "textBox1"
with "textBox2",
but my method has no way of knowing that "textBox1" is
the
text that must be replaced, since the entire string comes
to it.

How do I replace a value when I don't know what the value
is?

I don't think I want to use a regex because of the speed
penalty in instancing the class over and over.

Thanks.

Nov 18 '05 #2
Hi localhost,

Thank you for using Microsoft Newsgroup Service. Based on your
description,it seems that you want to determine which control to be
operated flexibly via code rather than hard code it in script. please
correct me if I misunderstand your problem.

If my understand is correct, here is my suggestion:

In client java script, you can dynamicly find a control object via his id,
just use the
document.All(id) method. For example , if there are serveral textboxes on a
page, just like:

<form id="Form1" method="post" runat="server">
<INPUT id="txt1" type="text">
<INPUT id="txt2" type="text">
<INPUT id="txt3" type="text">
<INPUT id="txt4" type="text">
<INPUT id="txt5" type="text">
</form>
you can then write such a javascript function:
<script language="javascript">
function btnSelect_click( txtID)
{

var objText = document.all(txtID)

if(objText != null)
{
objText.focus()
objText.select()
}

}
</script>

Thus, you can use the function in your client code to select and set focus
on a certain textbox or even other control alternatively. No regex needed!
Please try out the preceding suggestions and let me know whether they help.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #3
localhost wrote:
I have a string that looks like this:

"document.form1.textBox1.focus
();document.form1.textBox1.select();"
I want to replace the text between "document.form1."
and ".focus()",
as well as the text betwen "document.form1." and ".select
()".

Ultimately the effect is replacing "textBox1"
with "textBox2",
but my method has no way of knowing that "textBox1" is
the
text that must be replaced, since the entire string comes
to it.

How do I replace a value when I don't know what the value
is?

I don't think I want to use a regex because of the speed
penalty in instancing the class over and over.

Thanks.


If the only reason you're not using a regex is because you don't want to
'compile' the regex pattern over and over, then simply instance the
regex once, and place a reference to it in a static (ie., implement it
using the Singleton pattern).

Of course, this assumes that you always want to use the same regex
pattern (which is not clear from your description above).

--
mikeb

Nov 18 '05 #4

Steven, I am afraid that you have completely
misunderstood my question.

I am not looking for a client-side code solution. I am
looking for a way to replace part of a string in a (C#)
code-behind. The string happens to be the value of the
onLoad attribute in a body tag.

I have a string that looks like this:

"init();this.docForm.textBox1.focus
();this.docForm.textBox1.select();"

In this case, I need to replace "textBox1"
with "textBox2". However, the value-to-replace may not
be "textBox1", it could be anything. What kind of RegEx
would I need to replace a value between other parts of a
string (in this case between "docForm." and a ".")?
Thanks.

-----Original Message-----
Hi localhost,

Thank you for using Microsoft Newsgroup Service. Based on yourdescription,it seems that you want to determine which control to beoperated flexibly via code rather than hard code it in script. pleasecorrect me if I misunderstand your problem.

If my understand is correct, here is my suggestion:

Nov 18 '05 #5
Hi A.M,

Thank you for the reply. I'm sorry for misunderstanding your problem. Your
problem is to find a method to replace a string with a certain format. In
your case, the string you metioned is such as
"document.form1.txtName.select()", I think maybe you can try the Split()
and Join method of the dotnet String class. The two methods can split a
string into string array via a specified char, and combine a string array
via a certain separator string.

For example, you have a string like the "document.form1.txtName.select()",
and you want to replace the "txtName" by other string, you can code as this:

private string ReplaceObject(string exp, int pos, string sep, string newval)
{
string[] arr = exp.Split(sep.ToCharArray());
arr[pos] = newval;

return string.Join(sep,arr);
}
thus, you can use it in your code like this:

string original = "document.form1.txtName.focus()";

string result = ReplaceObject(original, 2, ".", "txtEmail");

you will get the result= "document.form1.txtEmail.focus()"
Also, this method can be used for other replacing situation. You can try
the mehotd out to see whether it helps you. If you still feel it unsuitable
for your problem, I think you can think about using the Regex.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #6
Hi Peter,

Is my suggestion helpful to you? Have you resolved your problem? Please let
me know if you have any thing unclear on it.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Roose | last post: by
4 posts views Thread by higabe | last post: by
22 posts views Thread by Phlip | last post: by
8 posts views Thread by pras.vaidya | last post: by
5 posts views Thread by djc | last post: by
9 posts views Thread by howachen | last post: by
11 posts views Thread by windandwaves | last post: by
4 posts views Thread by raylopez99 | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.