Lets say I have two strings:
string s1 = "hello";
string s2 = "hello world";
Is there a way I can get a string s3 = " world";
which is the difference between the 2 strings?
EDIT:
The difference will be always in this scenario
s1 = "abc"
s2 = "abcd ads as "
The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.
In C, string values (including string literals) are represented as arrays of char followed by a 0 terminator, and you cannot use the == operator to compare array contents; the language simply doesn't define the operation.
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
Use string s3 = s2.Replace(s1, "");
EDIT: Note that all occurrences of s1
in s2
will be absent from s3
. Make sure to carefully consider the comments on this post to confirm this is your desired result, for example the scenarios mentioned in @mellamokb's comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With