[英]Delete everything after part of a string
我有一個由三部分組成的字符串。 我希望字符串是(更改),一個單獨的部分(不更改)和更改的最后一部分。 我想刪除分隔部分和結尾部分。 分隔部分是“ - ”,所以我想知道是否有辦法刪除字符串的某個部分之后的所有內容。
這種情況的一個例子是,如果我想把這個:“Stack Overflow - A place to ask stuff”變成這個:“Stack Overflow”。 任何幫助表示贊賞!
例如,你可以這樣做:
String result = input.split("-")[0];
要么
String result = input.substring(0, input.indexOf("-"));
(並添加相關的錯誤處理)
apache commons StringUtils 提供了一個substringBefore方法
StringUtils.substringBefore("Stack Overflow - A place to ask stuff", " - ")
你可以用這個
String mysourcestring = "developer is - development";
String substring = mysourcestring.substring(0,mysourcestring.indexOf("-"));
它會寫成“開發者是——”
使用內置的 Kotlin substringBefore
函數( 文檔):
var string = "So much text - no - more"
string = string.substringBefore(" - ") // "So much text"
它還有一個可選的第二個參數,如果沒有找到分隔符,它是返回值。 默認值為原始字符串
string.substringBefore(" - ", "fail") // "So much text"
string.substringBefore(" -- ", "fail") // "fail"
string.substringBefore(" -- ") // "So much text - no - more"
也許這就是你要找的:
String str="Stack Overflow - A place to ask stuff";
String newStr = str.substring(0, str.indexOf("-"));
我為所有方法創建了示例程序, SubString
似乎是最快的程序。
Using builder : 54
Using Split : 252
Using Substring : 10
下面是示例程序代碼
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;
安全刪除直到字符串的干凈方法,如果令牌可能存在或可能不存在,則保留搜索的部分。
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函數是空、空和無匹配安全的
這將做你需要的:
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);
你可以用我的 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.