Tricky Replace String Question

L

localhost

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.
 
A

Alvin Bruney

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
 
M

MSFT

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.)
 
M

mikeb

localhost said:
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).
 
L

localhost

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.
 
M

MSFT

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.)
 
M

MSFT

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top