首页 \ 问答 \ 通过索引获取字符串字符 - Java(Get string character by index - Java)

通过索引获取字符串字符 - Java(Get string character by index - Java)

我知道如何计算字符串中某个字符或数字的索引,但是有什么预定义的方法可以用来给我第n个位置的字符? 所以在字符串“foo”中,如果我要求索引为0的字符,它将返回“f”。

注意 - 在上述问题中,“字符”我不是指char数据类型,而是字符串中的字母或数字。 这里重要的是我调用该方法时不会收到一个char,而是一个长度为1的字符串。 我知道substring()方法,但是我想知道是否有一个更整齐的方式。


I know how to work out the index of a certain character or number in a string, but is there any predefined method I can use to give me the character at the nth position? So in the string "foo", if I asked for the character with index 0 it would return "f".

Note - in the above question, by "character" I don't mean the char data type, but a letter or number in a string. The important thing here is that I don't receive a char when the method is invoked, but a string (of length 1). And I know about the substring() method, but I was wondering if there was a neater way.


原文:https://stackoverflow.com/questions/11229986
更新时间:2024-01-16 22:01

最满意答案

哪个最适合你,取决于你的需要,欲望和工作流程。 他们在某种意义上是半同构的,只是一些比别人更容易使用,用于特定的任务。

  • 当您在与子项目不同的时间内在子项目上进行控制和开发时, gitslave非常有用,此外,当您通常要同时标记,分支,推,拉等所有存储库时。 gitslave从来没有在我知道的Windows上测试过。 它需要perl。

  • 当您不控制子项目或更具体地希望将子项目修改为特定版本时,即使子项目发生变化, git-submodule也会更好。 git-submodule是git的标准部分,因此可以在windows上工作。

  • git-subtree为git的内置子树合并策略提供了前端。 当您更喜欢拥有单一存储库“统一”git历史记录时,情况会更好。 与子树合并策略不同的是,将更改导出到不同的(目录)树更简单的原始项目,但它不像gitslave甚至git-submodule那样自动。

  • 在理论上, 回购与gitslave相似,但并没有记录我发现的非android操作。 它相当致力于Google Android开发模式,并且只能原生支持少量的git命令(尽管可以运行任意命令),并且有限的本地支持不支持,例如,集中的存储库来推送和检出分支似乎相当困难。

  • 如果您使用多个版本控制系统,kitenet的mr将是您想要使用的,但是由于其最低的公分母方法,因此主要限于git-only超级项目。 有些方法可以运行任意命令,但是它们的集成度并不理想。


Which is best for you depends on your needs, desires, and workflow. They are in some senses semi-isomorphic, it is just some are a lot easier to use than others for specific tasks.

  • gitslave is useful when you control and develop on the subprojects at more of less the same time as the superproject, and furthermore when you typically want to tag, branch, push, pull, etc all repositories at the same time. gitslave has never been tested on windows that I know of. It requires perl.

  • git-submodule is better when you do not control the subprojects or more specifically wish to fix the subproject at a specific revision even as the subproject changes. git-submodule is a standard part of git and thus would work on windows.

  • git-subtree provides a front-end to git's built-in subtree merge strategy. It is better when you prefer to have a single-repository "unified" git history. Unlike the subtree merge strategy, it is easier to export changes to the different (directory) trees back out to the original project, but it is not as automatic as it is with gitslave or even git-submodule.

  • repo is in theory similar to gitslave, but not as well documented for non-android operations that I have found. It is fairly dedicated to the Google Android development model and only natively supports a handful of git commands (though you can run arbitrary commands) and the limited native support doesn't support, for example, a centralized repository to push to and checking out a branch seems fairly difficult.

  • kitenet's mr is what you would want to use if you have multiple version control systems in use, but is mostly limited for git-only superprojects due to its lowest common denominator approach. There are ways to run arbitrary commands, but they are not as well integrated.

相关问答

更多
  • 如果可能,最好确保.gitmodules文件包含可供任何人克隆的存储库URL,通常为git://或http:// URL。 然后,拥有SSH访问权限的用户可以在克隆后更改为子模块,并将remote.origin.url的URL更改为使用其用户名指向SSH URL,例如: cd my-submodule git remote set-url origin otheruser@server:/pathtorepos 即使在当前情况下,其他用户也应该能够这样做。 更新: Chris Johnsen指出,如果 ...
  • 首先,确保您的子模块文件夹包含以下内容: git submodule update --init --recursive 然后,只需: git submodule foreach git tag -l 对于每个子模板,您应该看到tagName 。 意味着您之前的命令确实标记了这些子模块。 我建议制作带注释的标签 ,而不是轻量级 标签 : git submodule foreach git tag -m "tagName" tagName 这意味着您可以从每个子模块中推送该标签。 如果你只是在父级回购 ...
  • 哪个最适合你,取决于你的需要,欲望和工作流程。 他们在某种意义上是半同构的,只是一些比别人更容易使用,用于特定的任务。 当您在与子项目不同的时间内在子项目上进行控制和开发时, gitslave非常有用,此外,当您通常要同时标记,分支,推,拉等所有存储库时。 gitslave从来没有在我知道的Windows上测试过。 它需要perl。 当您不控制子项目或更具体地希望将子项目修改为特定版本时,即使子项目发生变化, git-submodule也会更好。 git-submodule是git的标准部分,因此可以在wi ...
  • 如回顾性地添加 - 对git repo的反馈 git submodule update --init --recursive 应该工作 As mentioned in Retrospectively add --recursive to a git repo git submodule update --init --recursive should work.
  • 看起来你并没有将你的子模块推到你的主项目上。 只要推他们。 如果克隆应包含子模块的存储库,但在根级别没有.gitmodules ,则意味着远程服务器没有注册它们。 首先确保子模块被推送到服务器。 您需要将子模块添加到父项目中(仅克隆或在另一个回购中创建回购是不够的)。 It seems that you didn't actually pushed your submodules on your master project. Just push them. If you clone a repositor ...
  • 如果somedir , someotherdir和thewanteddir已经是一个repo的一部分,那么你就不能直接看到thewanteddir (最好你会选择' theSubmoduleRootDir/thewanteddir ') ( 浅层克隆子模块不会只隔离一个子目录) 最干净的解决方案(但不是最简单的解决方案)是将子模块仓库拆分为两个 ,并使sourcesdir成为自己的仓库(然后可以将其添加为子模块) If somedir, someotherdir and thewanteddir are a ...
  • 内部git跟踪文件夹不存在于s中,而是列出了类似链接的条目,其中包含像sha哈希的字母数字 那些不是子模块,而是简单的gitlink ( 索引中的特殊条目 ),代表嵌套的git repo SHA1。 请参阅“ 嵌套git repos和子模块之间的区别 ”。 有什么方法可以从我的主要git仓库中恢复我的更改,还是他们永远丢失了? 至少克隆你的repo(你推送gitlinks +你的代码的那个):你会在那里找到你的变化。 然后从一个新的回购开始,并强制推动。 有没有办法让内部仓库(现在是一个链接)到我将代码推送 ...
  • 我会推荐android使用的repo工具。 它足够通用,可以与任何git托管环境一起使用,并且不需要超级项目提交来更新像子模块那样的子项目。 首先,按照此处所述安装客户端: https : //source.android.com/source/downloading.html#installing-repo 然后创建一个清单存储库。 清单是一个xml文件,它描述了应该检出的git存储库位置和路径。 喜欢这个: mkdir manifests cd manifests git init 创建清单文件def ...
  • -dirty后缀表示子模块已更改。 做这个: cd wp-content/plugins/advanced-custom-fields 然后 git status 并且您将看到哪些更改导致您的子模块变脏。 the -dirty suffix means the submodule has changed. do this: cd wp-content/plugins/advanced-custom-fields then git status and you'll see what changes ...
  • 子模块非常适合: 引用另一个repo的一个特定提交(真正相当于带有显式修订号的svn external), 保持两个历史(父回购和外部回购) 分开 (如基于组件的方法 )。 子树非常适合将一个回购的历史包含在另一个回购中。 因此,如果那些少数外部存储库没有最终使用主父父级的所有标记和提交历史记录的职业,请使用子模块。 否则,子树很好。 submodule is great to: reference one specific commit of another repo (true equivalent o ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)