Thread overview
String Comparison Operator
Apr 30, 2017
Jolly James
Apr 30, 2017
Apr 30, 2017
Xinok
Apr 30, 2017
bauss
Apr 30, 2017
ag0aep6g
Apr 30, 2017
Xinok
Apr 30, 2017
tcak
April 30, 2017
Is there a String Comparison Operator in D?
April 30, 2017
On Sunday, 30 April 2017 at 15:31:39 UTC, Jolly James wrote:
> Is there a String Comparison Operator in D?

~

April 30, 2017
On Sunday, 30 April 2017 at 15:31:39 UTC, Jolly James wrote:
> Is there a String Comparison Operator in D?

Yeah, just the usual comparison operators:

"abc" == "abc"
"abc" != "ABC"


~ is for string concatenation, i.e.:

"abc" ~ "def" == "abcdef"
April 30, 2017
On Sunday, 30 April 2017 at 15:31:39 UTC, Jolly James wrote:
> Is there a String Comparison Operator in D?

You normally use double equation marks (==) to do that.

auto name = "Jack";
if( name == "Jack" ) writeln("Hi Jack!");
April 30, 2017
On Sunday, 30 April 2017 at 16:15:41 UTC, Xinok wrote:
> On Sunday, 30 April 2017 at 15:31:39 UTC, Jolly James wrote:
>> Is there a String Comparison Operator in D?
>
> Yeah, just the usual comparison operators:
>
> "abc" == "abc"
> "abc" != "ABC"
>
>
> ~ is for string concatenation, i.e.:
>
> "abc" ~ "def" == "abcdef"

Just to clarify.

It's not actually a string concatenation operator, it's an array appending operator.

Strings are just an alias for immutable(char)[] and not actually a type unlike other languages like C#, Java etc. where strings are objects.

In fact it doesn't have any operators that doesn't work with any other type of arrays. Just like functions such as replace etc. aren't necessarily string functions, but works with any type of arrays.
April 30, 2017
On 04/30/2017 09:05 PM, bauss wrote:
> On Sunday, 30 April 2017 at 16:15:41 UTC, Xinok wrote:
[...]
>> ~ is for string concatenation, i.e.:
[...]
> It's not actually a string concatenation operator, it's an array
> appending operator.

Appending is related but distinct. `~` does concatenation. `~=` does appending.

https://dlang.org/spec/arrays.html#array-concatenation

> Strings are just an alias for immutable(char)[] and not actually a type
> unlike other languages like C#, Java etc. where strings are objects.

I get what you mean, but while we're splitting hairs: `string` definitely is a type. It's the same type as `immutable(char)[]`.

> In fact it doesn't have any operators that doesn't work with any other
> type of arrays. Just like functions such as replace etc. aren't
> necessarily string functions, but works with any type of arrays.

Not an operator, but `foreach` has special support for transcoding between the different UTF variants.

Regarding functions, narrow strings (`string`, `wstring`) are special cased all over phobos. It's because as ranges they have dchar elements, but as arrays they have char/wchar elements. std.array.replace [1] also mentions strings in its signature because of this.



[1] https://dlang.org/phobos/std_array.html#.replace
April 30, 2017
On Sunday, 30 April 2017 at 19:05:18 UTC, bauss wrote:
> On Sunday, 30 April 2017 at 16:15:41 UTC, Xinok wrote:
>> On Sunday, 30 April 2017 at 15:31:39 UTC, Jolly James wrote:
>>> Is there a String Comparison Operator in D?
>>
>> Yeah, just the usual comparison operators:
>>
>> "abc" == "abc"
>> "abc" != "ABC"
>>
>>
>> ~ is for string concatenation, i.e.:
>>
>> "abc" ~ "def" == "abcdef"
>
> Just to clarify.
>
> It's not actually a string concatenation operator, it's an array appending operator.
>
> Strings are just an alias for immutable(char)[] and not actually a type unlike other languages like C#, Java etc. where strings are objects.
>
> In fact it doesn't have any operators that doesn't work with any other type of arrays. Just like functions such as replace etc. aren't necessarily string functions, but works with any type of arrays.

Regarding concatenation vs appending, it's kind of both depending on the type of the operands. What I mean is all of the following are valid:

[10, 20] ~ [30, 40] == [10, 20, 30, 40]  // Concatenation
[10, 20] ~ 30       == [10, 20, 30]      // Appending
10 ~ [20, 30]       == [10, 20, 30]      // Prepending