473,382 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1465
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
8
by: pras.vaidya | last post by:
Hi , below given question was asked to me during an interview and i figured it out little tricky . It would be a great help if anyone could solve it. Code : - main() { char...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
9
by: howachen | last post by:
Hi, I have one very simple tricky question which is quite interesting, I would like to share with all of you here... //======================================= <script...
11
by: windandwaves | last post by:
Hi Folk I need to write a tricky replacement function. C = replace A with B in C C = replace D with E in C examples of A could be "a cat climbs a tree", examples of B could be
4
by: raylopez99 | last post by:
Why is the same variable local inside a 'foreach' loop yet 'global' in scope (or to the class) outside it? RL class MyClass { int MyMemberArray1; //member variables, arrays, that are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.