Click here to Skip to main content
15,513,489 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I am NOT asking how to compare strings.
I am asking how to retrieve / get the actual difference between them.
Is indexing thru each character and looking for non matching ones the only way to do this ?

What I have tried:

I did try to use regular expression
Posted
Updated 16-Aug-22 9:00am
v2
Comments
PIEBALDconsult 16-Aug-22 22:56pm    
Levenshtein Distance?
https://en.wikipedia.org/wiki/Levenshtein_distance

Quote:
Hiow do I get the difference of (two ) strings

First, you need to define what makes the difference between 2 strings.
See those 2 strings
ABCDEFGH
BCDEFGHA

Either you say that all characters are different at same position.
Either you say that first char is removed and a char is added at the end.

In fact the principle of the problem is related to : diff - Wikipedia[^]. This algorithm is crafted to work with files, but it is the same principle for dtrings.
Quote:
I did try to use regular expression

Probably not a good idea, RegEx are for a different usage.
 
Share this answer
 
Comments
Member 14968771 15-Aug-22 15:28pm    
Lopks as I did not ( blame it on user ) explain the task. So here is visual ( I do not do u-tube (

QString one = "Hello world";
QString two =" Hello";
The difference (QString) is "world". ( How about building QStringList (split by space) and compare ( I guess I cannot get away from plain comparing ) list items ? )
Patrice T 15-Aug-22 17:01pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
There can be many scenarios:
1. The identical part of the 2 strings is shifted. For example:
__ABC__
_ABC___
2. Strings with different length
other scenarios.

To begin with, I would create a list of "use cases" and the expected outcome, and only then, go to actually program it.
 
Share this answer
 
That kinda depends on what "difference between two strings" actually means.
If you have two strings "abcde" and "afbcd" is the difference a character by character comparison:
"b => f, c=> b, d => c, e => d
Or a more advance "inserted, deleted list":
"+f, -e"

What if the strings are different lengths? Does that make a difference?

Comparing strings to find actual differences can be pretty complicated, for example: Levenshtein distance - Wikipedia[^]
There is a list of different string metrics here: String metric - Wikipedia[^]

I'd say that the first thing you need to do is work out exactly what you mean by "difference", and then consider implementation after that!
 
Share this answer
 
v2
You can use strcmp (or wcscmp for UNICODE)

C++
if ( strcmp(string1,string2) != NULL) // these strings are different
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900