www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - String Comparison Operator

reply Jolly James <j.j jmail.com> writes:
Is there a String Comparison Operator in D?
Apr 30 2017
next sibling parent =?UTF-8?B?54Sh?= <mu mu.co.jp> writes:
On Sunday, 30 April 2017 at 15:31:39 UTC, Jolly James wrote:
 Is there a String Comparison Operator in D?
~
Apr 30 2017
prev sibling next sibling parent reply Xinok <xinok live.com> writes:
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"
Apr 30 2017
parent reply bauss <jj_1337 live.dk> writes:
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 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.
Apr 30 2017
next sibling parent ag0aep6g <anonymous example.com> writes:
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

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
Apr 30 2017
prev sibling parent Xinok <xinok live.com> writes:
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 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
Apr 30 2017
prev sibling parent tcak <6uqekg+anzstagnqueug sharklasers.com> writes:
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!");
Apr 30 2017