删除字符串的一部分之后的所有内容 [英] Delete everything after part of a string
问题描述
我有一个由三部分组成的字符串.我希望字符串是(更改),一个单独的部分(不更改)和更改的最后一部分.我想删除分隔部分和结尾部分.分隔部分是"- ",所以我想知道是否有办法删除字符串的某个部分之后的所有内容.
I have a string that is built out of three parts. The word I want the string to be (changes), a seperating part (doesn't change) and the last part which changes. I want to delete the seperating part and the ending part. The seperating part is " - " so what I'm wondering is if theres a way to delete everything after a certaint part of the string.
这种情况的一个例子是,如果我想把这个:Stack Overflow - A place to ask stuff"变成这个:Stack Overflow".任何帮助表示赞赏!
An example of this scenario would be if I wanted to turn this: "Stack Overflow - A place to ask stuff" into this: "Stack Overflow". Any help is appreciated!
推荐答案
例如,您可以这样做:
String result = input.split("-")[0];
或
String result = input.substring(0, input.indexOf("-"));
(并添加相关错误处理)
(and add relevant error handling)
这篇关于删除字符串的一部分之后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!