简体   繁体   English

删除字符串的一部分之后的所有内容

[英]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.分隔部分是“ - ”,所以我想知道是否有办法删除字符串的某个部分之后的所有内容。

An example of this scenario would be if I wanted to turn this: "Stack Overflow - A place to ask stuff" into this: "Stack Overflow".这种情况的一个例子是,如果我想把这个:“Stack Overflow - A place to ask stuff”变成这个:“Stack Overflow”。 Any help is appreciated!任何帮助表示赞赏!

For example, you could do:例如,你可以这样做:

String result = input.split("-")[0];

or要么

String result = input.substring(0, input.indexOf("-"));

(and add relevant error handling) (并添加相关的错误处理)

apache commons StringUtils 提供了一个substringBefore方法

StringUtils.substringBefore("Stack Overflow - A place to ask stuff", " - ")

You can use this你可以用这个

String mysourcestring = "developer is - development";
String substring = mysourcestring.substring(0,mysourcestring.indexOf("-"));

it would be written "developer is -"它会写成“开发者是——”

Kotlin Solution科特林解决方案

Use the built-in Kotlin substringBefore function ( Documentation ):使用内置的 Kotlin substringBefore函数( 文档):

var string = "So much text - no - more"
string = string.substringBefore(" - ") // "So much text"

It also has an optional second param, which is the return value if the delimiter is not found.它还有一个可选的第二个参数,如果没有找到分隔符,它是返回值。 The default value is the original string默认值为原始字符串

string.substringBefore(" - ", "fail")  // "So much text"
string.substringBefore(" -- ", "fail") // "fail"
string.substringBefore(" -- ")         // "So much text - no - more"

Perhaps thats what you are looking for:也许这就是你要找的:

String str="Stack Overflow - A place to ask stuff";

String newStr = str.substring(0, str.indexOf("-"));

I created Sample program for all the approches and SubString seems to be fastest one.我为所有方法创建了示例程序, SubString似乎是最快的程序。

Using builder : 54
Using Split : 252
Using Substring  : 10

Below is the sample program code下面是示例程序代码

            for (int count = 0; count < 1000; count++) {
        // For JIT
    }
    long start = System.nanoTime();
    //Builder
    StringBuilder builder = new StringBuilder(
            "Stack Overflow - A place to ask stuff");
    builder.delete(builder.indexOf("-"), builder.length());
    System.out.println("Using builder : " + (System.nanoTime() - start)
            / 1000);
    start = System.nanoTime();
    //Split
    String string = "Stack Overflow - A place to ask stuff";
    string.split("-");
    System.out.println("Using Split : " + (System.nanoTime() - start)
            / 1000);
    //SubString
    start = System.nanoTime();
    String string1 = "Stack Overflow - A place to ask stuff";
    string1.substring(0, string1.indexOf("-"));
    System.out.println("Using Substring : " + (System.nanoTime() - start)
            / 1000);
    return null;

Clean way to safely remove until a string, and keep the searched part if token may or may not exist.安全删除直到字符串的干净方法,如果令牌可能存在或可能不存在,则保留搜索的部分。

String input = "Stack Overflow - A place to ask stuff";
String token = " - ";
String result = input.contains(token)
  ? token + StringUtils.substringBefore(string, token)
  : input;
// Returns "Stack Overflow - "

Apache StringUtils functions are null-, empty-, and no match- safe Apache StringUtils函数是空、空和无匹配安全的

这将做你需要的:

newValue = oldValue.substring(0, oldValue.indexOf("-");
String line = "deltaasm:/u01/app/oracle/product/11.2.0/dbhome_1:N        # line addred by agent";

    String rep = "deltaasm:";
    String after = "";

    String pre = ":N";
    String aft = "";
    String result = line.replaceAll(rep, after);
    String finalresult = result.replaceAll(pre, aft);
    System.out.println("Result***************" + finalresult);

    String str = "deltaasm:/u01/app/oracle/product/11.2.0/dbhome_1:N        # line addred by agent";

    String newStr = str.substring(0, str.indexOf("#"));
    System.out.println("======" + newStr);

you can my utils method this action..你可以用我的 utils 方法这个动作..

public static String makeTwoPart(String data, String cutAfterThisWord){
    String result = "";

    String val1 = data.substring(0, data.indexOf(cutAfterThisWord));

    String va12 = data.substring(val1.length(), data.length());

    String secondWord = va12.replace(cutAfterThisWord, "");

    Log.d("VAL_2", secondWord);

    String firstWord = data.replace(secondWord, "");

    Log.d("VAL_1", firstWord);

    result = firstWord + "\n" + secondWord;


    return result;
}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM