Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two strings C#

Tags:

string

c#

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 "
like image 275
raym0nd Avatar asked Sep 22 '11 15:09

raym0nd


People also ask

Can you compare two strings in C?

The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.

Can I use == to compare strings in C?

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.

Can you use == to compare two strings?

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.


1 Answers

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.

like image 125
Paul Bellora Avatar answered Sep 19 '22 12:09

Paul Bellora