删除部分字符串后的所有内容 [英] Delete everything after part of a string

查看:110
本文介绍了删除部分字符串后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由三部分组成的字符串。我希望字符串是(改变),分离部分(不改变)和最后改变的部分。我想删除分离部分和结束部分。分离的部分是 - 所以我想知道的是,如果有一种方法可以删除字符串的某个部分之后的所有内容。

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。任何帮助表示赞赏!

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屋!

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