Any difference between : or , in the SubString() method?

introduction = "Hello World"

standard_greeting = SubString(introduction, 1:5)
println(standard_greeting) # Will print Hello.

standard_greeting = SubString(introduction, 1,5)
println(standard_greeting) # Will print Hello.
  1. Is there any difference between using , or : when using the SubString() method?

This is interesting, and I’m aware the Python is not Julia, but in Python, doing:

introduction = "Hello World"
print(introduction[1:5].strip()) # Will print ello

In Julia:

introduction = "Hello World"
standard_greeting = SubString(introduction, :5) # Will print o World
println(standard_greeting)

Python:

introduction = "Hello World"
print(introduction[:5].strip()) # Will print Hello
  1. In Julia, :5 is equivalent as saying start at index 5, is that the correct way of thinking about it?

The fact is :5 is actually parsed by Julia as the integer 5, so you’re using the method where only one integer is passed, which means “from position 5 on”. Don’t think that :5 represents any range, like Python does.

2 Likes

They look to be the same: https://github.com/JuliaLang/julia/blob/a62acdfae164a706328426af9ff9ca73601e28c4/base/strings/substring.jl#L38-L40

1 Like