-1

String = "9,3,5,*****,1,2,3"

I'd like to simply access "5", which is between two commas, and right before "*****"; then only replace this "5" to other value.

How could I do this in Java?

LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
  • Nothing. I know s.split; s.replace; s.substring; but How can I first split and then replace? – LookIntoEast Jul 22 '17 at 06:22
  • You should ask question based on what you have tried and the problem you are facing. You can use regex pattern like this String pattern = ".*(\\d),\\*+"; and create a on that regex Pattern r = Pattern.compile(pattern); now run matcher object against the text Matcher m = r.matcher(text); If you group by capture group after checking if any thing found by m.find() then m.group(1) will contain your value. Do Read RegEx & Capturing group in java.Check this SO question https://stackoverflow.com/questions/17969436/java-regex-capturing-groups – prajitgandhi Jul 22 '17 at 06:35

5 Answers5

3

You can try using the following regex replacement:

String input = "9,3,5,*****,1,2,3";
input = input.replaceAll("[^,]*,\\*{5}", "X,*****");

Here is an explanation of the regex:

[^,]*,    match any number of non-comma characters, followed by one comma
\\*{5}    followed by five asterisks

This means to match whatever CSV term plus a comma comes before the five asterisks in your string. We then replace this with what you want, along with the five stars in the original pattern.

Demo here:

Rextester

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

I'd use a regular expression with a lookahead, to find a string of digits that precedes ",*****", and replace it with the new value. The regular expression you're looking for would be \d+(?=,\*{5}) - that is, one or more digits, with a lookahead consisting of a comma and five asterisks. So you'd write

newString = oldString.replaceAll("\\d+(?=,\\*{5})", "replacement");

Here is an explanation of the regex pattern used in the replacement:

\\d+           match any numbers of digits, but only when
(?=,\\*{5})    we can lookahead and assert that what follows immediately
               is a single comma followed by five asterisks

It is important to note that the lookahead (?=,\\*{5}) asserts but does not consume. Hence, we can ignore it with regards to the replacement.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
1

I considered newstr be "6"

String str =  "9,3,5,*****,1,2,3";
char newstr = '6';
str = str.replace(str.charAt(str.indexOf(",*") - 1), newstr);

Also if you are not sure about str length check for IndexOutOfBoundException and handle it

Popeye
  • 253
  • 4
  • 13
0

You could split on , and then join with a , (after replacing 5 with the desired value - say X). Like,

String[] arr = "9,3,5,*****,1,2,3".split(",");
arr[2] = "X";
System.out.println(String.join(",", arr));

Which outputs

9,3,X,*****,1,2,3
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

you can use spit() for replacing a string

String str =  "9,3,5,*****,1,2,3";
String[] myStrings = str.split(",");
String str1 = myStrings[2];
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17