This is somehow a duplicate of this Ruby problem - replace the first occurrence of a substring with another string only in java.
Problem:
I have a line: "ha bla ha ha"
"ha bla ha ha"
Now I want to replace the first (and only the first) "ha"with "gurp":
"ha"
"gurp"
"gurp bla ha ha"
string.replace("ha", "gurp")doesn't work as it replaces all "ha"s.
string.replace("ha", "gurp")
Try the methodreplaceFirst . It uses regex, but the literal sequence "ha"still works.
replaceFirst
string.replaceFirst("ha", "gurp");
replaceFirst() ( Java 1.4), , :
replaceFirst()
string = string.replaceFirst("ha", "gurp");
You should use already tested and well-documented libraries in favor of writing your own code!
StringUtils.replaceOnce("aba", "a", "") = "ba"
(copied from How to replace a string only once without a regular expression in Java?)
Source: https://habr.com/ru/post/1527976/More articles:.NET Memory не освобождает даже после выхода из функции - c#https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1527972/workarround-to-allow-reserved-words-to-be-used-as-property-names-on-ie8&usg=ALkJrhhapnVYH3tZ57jPsR25hDh_h3eCigMemory problems in .NET. - c #Преобразование С# XSLT из памяти - c#Горизонтальная шкала времени в чистом css - htmlКак я получаю позицию символа из строки в партии - command-linePassing reset to SystemVerilog statements - system-verilogDoes WPF chart management exist by default or do I need to do an external download? - c #How to get the number of rows of an audit table using sleep mode - hibernateI keep getting the error "else without if" - javaAll Articles