如何在Javascript中的字符串中获取第n个字符? [英] How to get nth character in a string in Javascript?

查看:149
本文介绍了如何在Javascript中的字符串中获取第n个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串(让我们说Hello World),我想将它的第一个字符保存在不同的变量中(例如,字符1 =H,字符2 =e......)。

I've a string (let's say "Hello World") and I want to save the first characters of it in different variables (ex. character1 = "H", character2 = "e"...).

如何获得字符串中的第n个字符?

How do I get the nth character in the string?

谢谢!

奖金:
我怎么能用Python做到这一点?它会比客户端更好吗?

Bonus: How could I do it with Python? Would it be better than doing it client-side?

推荐答案

使用 charAt 方法。就客户端或服务器而言,它取决于上下文。但你不应该信任客户输入。

Use the charAt method. As far as client or server, it depends on the context. But you should not trust client input.

这篇关于如何在Javascript中的字符串中获取第n个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆