473,473 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String manipulation questions

Hi,

Replacing strings in a text (likely an XML) file. Some newbie
questions...

....
while line:
counter=counter+1
if line.find(newstring) != -1:
print 'match at line'+str(counter)
newline = line.replace(oldstring, newstring)
fileOUT.write(newline)
line=fileIN.readline()
.....

Question1: The replace method - If a string does not have the target
replacement "newstring", then newline equals oldstring? Ie. oldstring
is not changed in any way? Seems to be what I observe but just want to
confirm this.

Question2: I'm using "line.find(newstring) != -1..." because I want
to print when a replacement happens. Does "line.replace..." report
indirectly somehow when it replaces?
Thanks

P.S. I know I should be using XSLT to transform XML - but the above
seems to work for small text changes.
Apr 9 '08 #1
2 1164
goldtech <go******@worldpost.comwrote:
Question1: The replace method - If a string does not have the target
replacement "newstring", then newline equals oldstring? Ie. oldstring
is not changed in any way? Seems to be what I observe but just want to
confirm this.
Yes.
>
Question2: I'm using "line.find(newstring) != -1..." because I want
to print when a replacement happens. Does "line.replace..." report
indirectly somehow when it replaces?
Just do the line.replace() and then test for a change.

Question 3 (which I'm sure you meant to ask really) ...
No, you shouldn't be using a while loop here. Use a for loop and the
enumerate builtin:

for counter, line in enumerate(fileIN):
newline = line.replace(oldstring, newstring)
if newline != line:
print 'match at line', counter+1
fileOUT.write(newline)
Apr 9 '08 #2
snip...
>
for counter, line in enumerate(fileIN):
newline = line.replace(oldstring, newstring)
if newline != line:
print 'match at line', counter+1
fileOUT.write(newline)
"enumerate" - haven't seen that before. Nice!

Thanks
Apr 9 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: I. Kobrinsky | last post by:
I'm new here. I started a personal password-program, a trial that includes username, logincounter and password. So my intention is to hide pwd while tipping. So I'm thinking about two popular...
9
by: stefan.oedenkoven | last post by:
Hi ng, could you tell me whats the common way to manipulate Strings in C/C++. I'm a little bit confused, 'cause there are too many ways to do it ;-) In my example i would like to open a file,...
15
by: Beeeeeves | last post by:
Is there a quick way to find the index of the first character different in two strings? For instance, if I had the strings "abcdefghijkl" and "abcdefxyz" I would want the return value to be...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
9
by: Michael D. Ober | last post by:
OK, I can't figure out a way to optimize the following VB 2005 code using StringBuilders: Public Const RecSize as Integer = 105 Private buffer As String Public Sub New() init End Sub...
5
by: Joe Nova | last post by:
I'm a C++ noob and I need a little help manipulating strings. I've got a program that takes an expression in the form: "operand1 operator operand2" I'd like to: 1. Find the total length...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
0
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,...
1
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...
0
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.