# Detect space

**URL:** https://discourse.julialang.org/t/detect-space/44521
**Category:** New to Julia
**Created:** [August 7, 2020, 7:08pm UTC](https://discourse.julialang.org/t/detect-space/44521 "2020-08-07T19:08:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)
#### Post date: [August 7, 2020, 7:08pm UTC](https://discourse.julialang.org/t/detect-space/44521/1 "2020-08-07T19:08:47Z")

</div>

hi all  
i try to detect space in string, if i have

> “Hello World , i need to detect the index i , in python i just look for if arr[i] ==” "  
> put here it doesn’t work.

```julia
function lengthOfLastWord(arr)
    i=length(arr)
    count = 0 
    while i>=1 && arr[i] ==" "
        print(arr[i])
        i -=1
    end
    while i>=1 && arr[i] !=" "
        i-=1
        count +=1
    end
    return count
end

arr = "Hello World"
println(lengthOfLastWord(arr))

```

the output her = 11  
it mean he reverse to the beginning and skip on the space between two word.  
it should return 5

any help ? thanks a lot for u guys you are helping me a lot

---

<div class="post-metadata">

### Author: ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)
#### Post date: [August 7, 2020, 7:14pm UTC](https://discourse.julialang.org/t/detect-space/44521/2 "2020-08-07T19:14:56Z")

</div>

> [@Fuad\_Sami](#):
>
> `arr[i] ==" "`

Maybe try this instead:

```julia
arr[i] == ' '

```

---

<div class="post-metadata">

### Author: ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)
#### Post date: [August 7, 2020, 7:19pm UTC](https://discourse.julialang.org/t/detect-space/44521/3 "2020-08-07T19:19:59Z")

</div>

work !  
thanks

---

<div class="post-metadata">

### Author: ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)
#### Post date: [August 7, 2020, 7:22pm UTC](https://discourse.julialang.org/t/detect-space/44521/4 "2020-08-07T19:22:33Z")

</div>

You also may be interested in the [`isspace`](https://docs.julialang.org/en/v1/base/strings/#Base.Unicode.isspace) function:

```julia
isspace(arr[i])

```

---

<div class="post-metadata">

### Author: ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)
#### Post date: [August 7, 2020, 8:15pm UTC](https://discourse.julialang.org/t/detect-space/44521/5 "2020-08-07T20:15:11Z")

</div>

It is worth noting that [strings in Julia can use variable-length encoding](https://docs.julialang.org/en/v1/manual/strings/#Unicode-and-UTF-8-1), which means that `1:length(str) == collect(eachindex(str))` may be false.

For example, when applying your function to string `"hello ωrold"`, the result is a `StringIndexError`. A generic implementation of your function is:

```julia
function lengthOfLastWord(str)
	i = findlast(isspace, str)
	if i === nothing
		length(str)
	else
		length(str, nextind(str, i), lastindex(str))
	end
end

```
