keep learning, keep coding!
Problem - Length of Last Word
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
翻譯蒟蒻
要回傳整個字串中的最後一個字母的長度。
Example 1:
1
2
3Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.Example 2:
1
2
3Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.Example 3:
1
2
3Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
Solution - JavaScript
1 | /** |
Solution - Ruby
1 | def length_of_last_word(s) |
Solution - PHP
1 | function lengthOfLastWord($s) { |
PHP 方法筆記
trim
用於刪除字串開頭和結尾空白字符函數。這些空白字符包括空格、換行符、制表符等。1
2
3
4
5
6
7
8$str = " Hello, World! ";
$trimmedStr = trim($str);
echo "原始字串: '$str'\n";
echo "刪除空白後的字串: '$trimmedStr'";
// Output: 原始字串: ' Hello, World! '
// Output: 刪除空白後的字串: 'Hello, World!'