-1
String str = "hdfCity1kdCity12fsd". 

I want to replace only City1 with Goa without replacing City1 sequence in City1X in above String.

I tried using replace function.

str = str.replace("City1", "Goa")

but the result is

str = "hdfGoakdGoa2fsd"

how to do this selective replace? to get this desired result

str = "hdfGoakdCity12fsd";//solution: str.replaceAll("(?<!\\d)City1(?!\\d)", "Goa");

sorry for making my case not clear

Thanks @TiiJ7

  • 3
    Please edit & add a tag for the language you are using. – Alex K. Jul 23 '19 at 13:21
  • This doesn't look like javascript? String str = 'some text' is not valid – Doğancan Arabacı Jul 23 '19 at 13:22
  • @Roy -- Allow OP to set language. Also, can't be javascript as that would provide correct result. – Asons Jul 23 '19 at 13:23
  • This looks like Java. – Roberto Zvjerković Jul 23 '19 at 13:23
  • thanks for your interest in this, the exact case is similar to like this "City1****City12" where * is any thing except Int charector and I'm looking for generic code like replacing CityX(where), not just City1 and City12. #stars added before City1 and after City2 are not visible after i submitted my comment – Slowdeath451 Jul 24 '19 at 08:33
  • @TiiJ7 Not working, result is same as initial string. – Slowdeath451 Jul 24 '19 at 09:43
  • 1
    Yes, in that case your situation is slightly different from the one I linked. You'll need something like `str.replaceAll("(?<!\\d)City1(?!\\d)", "Goa")`. You need to check if it works for all your cases, though. ([test online](https://regex101.com/r/WxqNbr/2) if you want) – TiiJ7 Jul 24 '19 at 09:54
  • @TiiJ7 It's working, big load of thanks. – Slowdeath451 Jul 24 '19 at 11:59

5 Answers5

2

In your case you could use replaceFirst(). This will only replace the first occurence of your matched String:

String str = "City1 is beautiful than City12";
str = str.replaceFirst("City1", "Goa");
System.out.println(str);

Will output:

Goa is beautiful than City12

Other than that you could use a more sophisticated regex to match your exact case, see for example this answer.

T A
  • 1,677
  • 4
  • 21
  • 29
  • What if the string is "City12 is beautiful than City1"? – Joakim Danielson Jul 23 '19 at 13:33
  • In this case OP would need a regex, I linked the answer below mine. – T A Jul 23 '19 at 13:36
  • @JoakimDanielson you might be right though if that's what OP intended this should be closed as a duplicate. – T A Jul 23 '19 at 13:39
  • That is what I voted for. – Joakim Danielson Jul 23 '19 at 13:40
  • thanks for your interest in this, the exact case is similar to like this "***City1****City12***" where * is any thing except Int charector and I'm looking for generic code like replacing CityX(where), not just City1 and City12. #stars added before City1 and after City2 are not visible after i submitted my comment – Slowdeath451 Jul 24 '19 at 07:22
  • If your statement is *always* formulated like this and you *always* want to replace the first city name, `replaceFirst()` is your best option. – T A Jul 24 '19 at 07:42
  • @Slowdeath451 My solution should still work after your edit. The contents before and after the matched String have no impact on the result. – T A Jul 24 '19 at 08:58
  • yes, replaceFirst() works fine for the case mentioned, but If order gets changed, the code function wont serve purpose. In my actual code i dont have any control how strings is ordered. – Slowdeath451 Jul 24 '19 at 09:16
  • Then you will have to do an exact match, see https://stackoverflow.com/questions/30183807/java-string-replace-exact-word/30183873#30183873 – T A Jul 24 '19 at 09:18
  • I'm thinking like this, I'll find the occurrences of City1 and for every occurrence ill check for the character at next index, if the character is not integer. I'll replace the occurrence with 'Goa'. – Slowdeath451 Jul 24 '19 at 09:22
0

you can use the method replaceFirst(regex, replacement) :

 String str = "City1 is beautiful than City12";
 System.out.println(str.replaceFirst("City1", "Goa")); // Goa is beautiful than City12

nader.h
  • 506
  • 2
  • 17
0

If it's just about the first part, you could also use the substring method. Example:

String str = "City1 is beautiful than City12";
str = "Goa" + str.substring(5);
waayne
  • 49
  • 6
0

If you're sure that City1 will not any characters around except whitespace you can use:

String str = "City1 is beautiful than than City12";
str = str.replace("City1 ", "Goa ");
System.out.println(str);

same as yours but additional space at the end of the replacing and new string

Joshgun
  • 372
  • 3
  • 16
0

You can use replaceFirst() or replaceAll() method, but if you want to replace in the middle, you can find the occurrence you are looking for (one example here: Occurrences of substring in a string)

Use the index returned to make 2 substrings: the first part remain unchanged and, in the second part, the first occurrence must be replaced (replaceFirst())

Last: join the two substrings.

Fabrizio R.
  • 117
  • 1
  • 14
  • Thanks, I'm thinking like this: ill find the occurences, for each occurence ill check for next character type, if it is integer ill replace the sequence. – Slowdeath451 Jul 24 '19 at 09:47