digitalmars.D.learn - Char[] confusing
- Qian Xu (30/30) Mar 02 2009 Hi,
- Lutger (5/40) Mar 02 2009 s[4] means the fifth element of s[]
- Qian Xu (11/16) Mar 02 2009 Thank you both.
- grauzone (2/19) Mar 02 2009 Think of it as "everything in the string before this."
- Rainer Deyke (8/9) Mar 02 2009 I tend to think of a indexes as referring to the positions between the
- Robert Fraser (5/21) Mar 03 2009 It's the same way it is in Python, etc. Also, it makes a lot of things
- bearophile (9/11) Mar 03 2009 At the beginning you have to think a bit about it, but you quickly learn...
- Stewart Gordon (5/11) Mar 05 2009 It might help to think of the indexes in a slice as numbering the
- BCS (8/15) Mar 02 2009 Having the fist number be included and the second not works better than ...
- Denis Koroskin (3/17) Mar 02 2009 That and also
Hi, I am confusing with getting sub-string of a char[]. ------------------------- code --------------------------------- module main; import tango.io.Console; import tango.text.convert.Integer; void main() { char[] s = "ABCDE"; // 5 chars int len = s.length; Cout("s='" ~ s ~ "', length=" ~ toString(len)).newline; Cout("s[" ~ toString(len-1) ~ "]= " ~ s[len-1]).newline; Cout("s[0 .. " ~ toString(len-1) ~ "]= " ~ s[0 .. len-1]).newline; Cout("s[0 .. " ~ toString(len) ~ "]= " ~ s[0 .. len]).newline; Cout("s[1 .. " ~ toString(len-1) ~ "]= " ~ s[1 .. len-1]).newline; Cout("s[1 .. " ~ toString(len) ~ "]= " ~ s[1 .. len]).newline; } ------------------------- code --------------------------------- The result is (dmd + windowsxp) s='ABCDE', length=5 s[4]= E s[0 .. 4]= ABCD s[0 .. 5]= ABCDE s[1 .. 4]= BCD s[1 .. 5]= BCDE ------------------------------------------------------- My question is: why s[4]=E, but s[0..4]=ABCD (without E) -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
Mar 02 2009
Qian Xu wrote:Hi, I am confusing with getting sub-string of a char[]. ------------------------- code --------------------------------- module main; import tango.io.Console; import tango.text.convert.Integer; void main() { char[] s = "ABCDE"; // 5 chars int len = s.length; Cout("s='" ~ s ~ "', length=" ~ toString(len)).newline; Cout("s[" ~ toString(len-1) ~ "]= " ~ s[len-1]).newline; Cout("s[0 .. " ~ toString(len-1) ~ "]= " ~ s[0 .. len-1]).newline; Cout("s[0 .. " ~ toString(len) ~ "]= " ~ s[0 .. len]).newline; Cout("s[1 .. " ~ toString(len-1) ~ "]= " ~ s[1 .. len-1]).newline; Cout("s[1 .. " ~ toString(len) ~ "]= " ~ s[1 .. len]).newline; } ------------------------- code --------------------------------- The result is (dmd + windowsxp) s='ABCDE', length=5 s[4]= E s[0 .. 4]= ABCD s[0 .. 5]= ABCDE s[1 .. 4]= BCD s[1 .. 5]= BCDE ------------------------------------------------------- My question is: why s[4]=E, but s[0..4]=ABCD (without E)s[4] means the fifth element of s[] s[0..4] is a slice from the first to the fifth, but not including the fifth element. The last element in a slice is always one past the end of that slice.
Mar 02 2009
Lutger wrote:s[4] means the fifth element of s[] s[0..4] is a slice from the first to the fifth, but not including the fifth element. The last element in a slice is always one past the end of that slice.Thank you both. I have to do math in mind in order to keep my code correct ;-) IMO, it does not make any sense. s[start..end] end - is neither the count of characters in the array slice, nor the end index of the slice. it is just the index after the real end character. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
Mar 02 2009
Qian Xu wrote:Lutger wrote:Think of it as "everything in the string before this."s[4] means the fifth element of s[] s[0..4] is a slice from the first to the fifth, but not including the fifth element. The last element in a slice is always one past the end of that slice.Thank you both. I have to do math in mind in order to keep my code correct ;-) IMO, it does not make any sense. s[start..end] end - is neither the count of characters in the array slice, nor the end index of the slice. it is just the index after the real end character.
Mar 02 2009
grauzone wrote:Think of it as "everything in the string before this."I tend to think of a indexes as referring to the positions between the characters instead of the characters themselves. "ABCD" -> 0 'A' 1 'B' 2 'C' 3 'D' 4 's[a..b]' = the elements betweens positions 'a' and 'b'. 's[a]' = the element to the right of position 'a'. -- Rainer Deyke - rainerd eldwood.com
Mar 02 2009
Qian Xu wrote:Lutger wrote:It's the same way it is in Python, etc. Also, it makes a lot of things easier, i.e.: s[0..x] is x characters long s[x..$] is everything from index x to the end of the strings[4] means the fifth element of s[] s[0..4] is a slice from the first to the fifth, but not including the fifth element. The last element in a slice is always one past the end of that slice.Thank you both. I have to do math in mind in order to keep my code correct ;-) IMO, it does not make any sense. s[start..end] end - is neither the count of characters in the array slice, nor the end index of the slice. it is just the index after the real end character.
Mar 03 2009
Qian Xu:I have to do math in mind in order to keep my code correct ;-) IMO, it does not make any sense.At the beginning you have to think a bit about it, but you quickly learn it, and you find it's the best way to design it :-) Several languages use this same convention. It allows you to split an array in two parts with very little troubles: s[0 .. $] == s[0 .. n] ~ s[n .. $] It's especially good when all the language uses this idea, for example, in D2, this loops ten times, and x never becomes 10: foreach (x; 0 .. 10) {...} Bye, bearophile
Mar 03 2009
Qian Xu wrote: <snip>IMO, it does not make any sense. s[start..end] end - is neither the count of characters in the array slice, nor the end index of the slice. it is just the index after the real end character.It might help to think of the indexes in a slice as numbering the boundaries between the array elements, rather than the elements themselves. Stewart.
Mar 05 2009
Reply to Qian,Hi, I am confusing with getting sub-string of a char[].[..]My question is: why s[4]=E, but s[0..4]=ABCD (without E)Having the fist number be included and the second not works better than the other options. Consider what would have to change to make these work for the the other options: arr[0 .. n] and arr[n .. arr.length] cover the full array arr[0 .. 0] is empty arr[0 .. arr.length] is the full array
Mar 02 2009
On Tue, 03 Mar 2009 02:03:23 +0300, BCS <ao pathlink.com> wrote:Reply to Qian,That and also assert(arr[a..b].length == (b - a)); // evaluates to true alwaysHi, I am confusing with getting sub-string of a char[].[..]My question is: why s[4]=E, but s[0..4]=ABCD (without E)Having the fist number be included and the second not works better than the other options. Consider what would have to change to make these work for the the other options: arr[0 .. n] and arr[n .. arr.length] cover the full array arr[0 .. 0] is empty arr[0 .. arr.length] is the full array
Mar 02 2009