Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 5: Strings #21

Open
sbromberger opened this issue Jan 7, 2015 · 3 comments
Open

Chapter 5: Strings #21

sbromberger opened this issue Jan 7, 2015 · 3 comments

Comments

@sbromberger
Copy link

In the interpolation section, you talk about * and $ constructs, and mention that $ is faster than *, but you don't mention that string(...) is fastest of all.

@sbromberger
Copy link
Author

Where a search string is not found, search() will yield 0:-1. That is an odd result, until you realise the reason: for any string s, s[0:-1] will necessarily yield "" (that is, an empty string).

Why is this the case? [1:-1] does the same thing, but [0:0] returns a BoundsError.

@chrisvoncsefalvay
Copy link
Owner

s[0,0] means 'give me all characters from the 0th to the 0th. Julia is 1-indexed, so you're asking for a non-existent index, and that's what Julia is trying to get at by the BoundsError.

@sbromberger
Copy link
Author

OK, but that doesn't explain why [0:-1] works. It's "give me all the characters from the 0th to the -1st", which seems even more wrong than [0:0].

Ah, this explains it:

julia> [0:0]
1-element Array{Int64,1}:
 0

julia> [0:-1]
0-element Array{Int64,1}

julia>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants