# 22. 最后一个单词的长度

java

```java
/**
 * 58. 最后一个单词的长度.
 * 给定一个仅包含大小写字母和空格 ' ' 的字符串，返回其最后一个单词的长度。
 *
 * 如果不存在最后一个单词，请返回 0 。
 *
 * 说明：一个单词是指由字母组成，但不包含任何空格的字符串。
 *
 * 示例:
 * 输入: "Hello World"
 * 输出: 5
 */
public class Solution58 {

    /**
     * 很简单，切分为数组，取最后一个元素即可
     * @param s
     * @return
     */
    public int lengthOfLastWord(String s) {
        if(s==null||s.length()==0) return 0;
        String[] a=s.trim().split(" +");
        if(a==null||a.length==0) return 0;
        return a[a.length-1].length();
    }

    public static void main(String[] args) {
        String s = "Hello World";
        int r = new Solution58().lengthOfLastWord(s);
        System.out.println(r);
    }
}
```

python

```
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/zhi-shang-lei/22-zui-hou-yi-ge-dan-ci-chang-du.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
