473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare two strings: Get all positions of differences

Hi All,

How do I compare two strings and get all the positions where there is a
difference between them?

Thanks,
Sasidhar
Nov 16 '05 #1
4 28036
Sasidhar,

You are going to have to do this manually. Basically, you would cycle
through each character in one string, and compare it to the corresponding
character in the other. If the string or the character doesn't exist, then
there is a difference (I assume the strings will be of different lengths).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Sasidhar Parvatham" <sa************ ****@gmail.com> wrote in message
news:Oo******** ******@TK2MSFTN GP15.phx.gbl...
Hi All,

How do I compare two strings and get all the positions where there is a
difference between them?

Thanks,
Sasidhar

Nov 16 '05 #2
Hi Sasidhar

This will do it (hope I'm not doing your homework :) and if so, make sure
you understand the code fully :P)

ArrayList list = new ArrayList();
string s = "Hello World";
string t = "Getto Word";

for(int i = 0; i < s.Length; i++)
{
if(i >= t.Length)
list.Add(i);
else if(s[i] != t[i])
list.Add(i);
}

// if you also want virtual positions if s is the shortest string
if(t.Length > s.Length)
{
for(int i = s.Length; i <= t.Length; i++)
list.Add(i);
}

// optional, the list will contain all positions
int[] indices = (int[])list.ToArray(t ypeof(int));

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
Thanks Nocholas and Morten.

I am aware of this method. I was looking for any other efficient way to do
this. Your responses confirmed that there is no better way to do this than
the one you guys suggested.

I was looking whether there is a way we can use FC (DOS file compare) to do
this or some other alternative.

Anyhow thank you once again for your responses.
Sasidhar
"Morten Wennevik" <Mo************ @hotmail.com> wrote in message
news:opsnhg32x6 klbvpo@pbn_comp uter...
Hi Sasidhar

This will do it (hope I'm not doing your homework :) and if so, make sure
you understand the code fully :P)

ArrayList list = new ArrayList();
string s = "Hello World";
string t = "Getto Word";

for(int i = 0; i < s.Length; i++)
{
if(i >= t.Length)
list.Add(i);
else if(s[i] != t[i])
list.Add(i);
}

// if you also want virtual positions if s is the shortest string
if(t.Length > s.Length)
{
for(int i = s.Length; i <= t.Length; i++)
list.Add(i);
}

// optional, the list will contain all positions
int[] indices = (int[])list.ToArray(t ypeof(int));

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4
" A Generic, Reusable Diff Algorithm in C# "

http://codeproject.com/csharp/DiffEngine.asp

HTH,
Alexander

"Sasidhar Parvatham" <sa************ ****@gmail.com> wrote in message
news:OM******** ******@TK2MSFTN GP15.phx.gbl...
Thanks Nocholas and Morten.

I am aware of this method. I was looking for any other efficient way to do
this. Your responses confirmed that there is no better way to do this than
the one you guys suggested.

I was looking whether there is a way we can use FC (DOS file compare) to
do
this or some other alternative.

Anyhow thank you once again for your responses.
Sasidhar
"Morten Wennevik" <Mo************ @hotmail.com> wrote in message
news:opsnhg32x6 klbvpo@pbn_comp uter...
Hi Sasidhar

This will do it (hope I'm not doing your homework :) and if so, make sure
you understand the code fully :P)

ArrayList list = new ArrayList();
string s = "Hello World";
string t = "Getto Word";

for(int i = 0; i < s.Length; i)
{
if(i >= t.Length)
list.Add(i);
else if(s[i] != t[i])
list.Add(i);
}

// if you also want virtual positions if s is the shortest string
if(t.Length > s.Length)
{
for(int i = s.Length; i <= t.Length; i)
list.Add(i);
}

// optional, the list will contain all positions
int[] indices = (int[])list.ToArray(t ypeof(int));

--
Happy Coding!
Morten Wennevik [C# MVP]


Nov 16 '05 #5

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

Similar topics

6
2226
by: Fazer | last post by:
Hello, I was wondering which would be the best way to compare a list? I was thinking of just using a for loop and testing the condition. What do you guys think? The best/fastest way of comparing lists. Thanks,
3
5807
by: jyoti | last post by:
Hi there, I am trying to compare two strings and get the differenc in vbscript. Example: Str1="This is a test text" Str2="This is a fine test text but a little long" I like to compare str1 with str2 and get the difference. Above example the difference will be "fine" and "but a little long" How do I do that? A sample code would be apprciable.
2
3346
by: Nathan Rosaaen | last post by:
I want to be able to easily see what changes were made from original to updated sentences (words added/removed/changed). I'm not too concerned about speed, just looking for something. The compared text will be anywhere from a word or two to multiple paragraphs. An example: Original sentence: The brown dog ran outside at night. During the day, the brown dog slept. Updated sentence: The big brown dog ran outside when dark. During...
4
52866
by: Maur | last post by:
Hi all, I have 2 tables say t_OLD and t_NEW. The new has corrections for audit purposes. They are identical in all respects (i.e. new is a copy of old and then changes are made to t_new) I would like a quick way to cycle through all the the fields in each table and compare the values to see if there are
5
10882
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the new database. For instance, her old database has a table with Band Info in it. Her new database also has a table with Band Info in it but slightly different. I was wondering if there was an easy way to compare the fields from similar tables in...
3
3694
by: Twinkle | last post by:
HI there i want to compare between two strings char by char.every strings having a word document.first string name is strFileName and second string name is strFilename1. i want to compare strFileName with strFileName1.if there is any mistek or some word is missing in second string then that words should be highlight(in sense change the color of that words) by the button click event. pls somebody give an idea to me.
0
1910
by: bobbymusic | last post by:
In my form I have two rich text boxes.In both of them I put two different text files(.txt or .rtf).I looking for some code to compare them, and to have the result as marked differences into the second rich text box.How I have to compare them-as strings or as files? And how I can use mid$ to compare them letter by letter to have the right positions of differences? Thanks in advance!!!
1
9491
by: Neilen Marais | last post by:
Hi I'm trying to compare some text to find differences other than whitespace. I seem to be misunderstanding something, since I can't even get a basic example to work: In : d = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK) In : list(d.compare(, )) Out:
1
3268
by: SusannaFloora | last post by:
I have to compare two XML strings.The difference present in the second string to be coloured in green.I dont want to use datasets. Is their any other way to solve this? Ex:String1 Ex:string2 Rabbit Rabbits Color Colour only the differences present in string2 s and u to be coloured...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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
10456
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...
0
9052
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
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
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
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.