-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?

2
  • Nothing. I know s.split; s.replace; s.substring; but How can I first split and then replace? Commented Jul 22, 2017 at 6: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 stackoverflow.com/questions/17969436/… Commented Jul 22, 2017 at 6:35

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

My apologies, @Tim. I messed up, then we clashed. Very sorry. I'm not really trying to be a vandal.
Many thanks. But could you explain a little bit about "\*{5}" ? What does it mean?
3

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.

1 Comment

thx. \\d+ means digits; while \*{5} means five asterisks.But i'm still vague about +(?= part; what does it mean?
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

Comments

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

2 Comments

I think OP is interested in finding the ***** and replacing the element before it, regardless of its position in the array.
@DawoodibnKareem between two commas, and right before "*****"; - so it seems the index is fixed (to me).
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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.