473,804 Members | 3,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tricky Replace String Question


I have a string that looks like this:

"document.form1 .textBox1.focus
();document.for m1.textBox1.sel ect();"
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 1487
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*******@coho rt.ces> wrote in message
news:03******** *************** *****@phx.gbl.. .

I have a string that looks like this:

"document.form1 .textBox1.focus
();document.for m1.textBox1.sel ect();"
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="javas cript">
function btnSelect_click ( txtID)
{

var objText = document.all(tx tID)

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.for m1.textBox1.sel ect();"
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.do cForm.textBox1. focus
();this.docForm .textBox1.selec t();"

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 misunderstandin g 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(s tring exp, int pos, string sep, string newval)
{
string[] arr = exp.Split(sep.T oCharArray());
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(o riginal, 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
2529
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' ) But to me this seems inefficient since the 'in' operation will search through the whole string, and then the 'replace' will as well.
4
62123
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 supposed to write this function? String.replace(/</g,'&lt;');
22
11124
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 replace baker replace charlie "
8
1885
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 *s1="abcd",*s2=NULL; /* From here you call a function copy which has return type void .
9
9161
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 StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
5
2444
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 to replace TAB's with "&nbsp;"'s to preserve the user's indentation. My questions are: 1) even though I'll probably look it up before I get a reply.... whats the vb code for TAB character? 2) more importantly; do I have to run this large field...
9
1725
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 type="text/javascript">
11
1934
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
2624
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 "global" to the class
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.