473,385 Members | 1,912 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,385 software developers and data experts.

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 28000
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.com

"Sasidhar Parvatham" <sa****************@gmail.com> wrote in message
news:Oo**************@TK2MSFTNGP15.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(typeof(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:opsnhg32x6klbvpo@pbn_computer...
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(typeof(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**************@TK2MSFTNGP15.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:opsnhg32x6klbvpo@pbn_computer...
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(typeof(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
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...
3
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...
2
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...
4
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) ...
5
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...
3
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...
0
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...
1
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 =...
1
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 ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.