0

I have an application which gathers wifi and cellular data usage and sends it as a string of data via SMS - however I need a bit of help modifying the string it sends into the correct format

The way it appears now:

USI;1;3056090866;06/16/58/06/24/13;CN25.48,WN86.957USI;CN34.931,WN16.656

The way I would like it to send in the following manner: (without the second USI in it)

USI;1;3056090866;06/16/58/06/24/13;CN25.48,WN86.957;CN34.931,WN16.656

How can this be accomplished? (I simply want to remove the 2nd occurance of the word USI but I'm not sure how you can programatically remove data using String.format)

CODE SNIPPET:

String info = String.format("USI%sCN%s,WN%s", tag + status + tag + mdn +tag + DToDevice + tag, mobileStr, totalStr + settings.getString("last_month", "0"));
4
  • 1
    Your two strings look identical to me. Did you forget to remove the second USI? Commented Jun 25, 2013 at 14:10
  • Wow... yes (no coffee yet!) Commented Jun 25, 2013 at 14:12
  • To my knowledge, you can't prevent only (the middle) parts of the input parameters from being output using String.format. You can, however, do a replace afterwards or change the applicable parameter appropriately. Commented Jun 25, 2013 at 14:16
  • Does this look correct? (It didn't seem to work as expected) String info = String.format("USI%sCN%s,WN%s", tag + status + tag + mdn +tag + DToDevice + tag, mobileStr, totalStr + settings.getString("last_month", "0")); info.replace(".+USI",""); Commented Jun 25, 2013 at 14:35

3 Answers 3

1

info.replace("USI","") will remove every instance of "USI". Maybe it would suffice to do "USI" + info.replace("USI","").

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

4 Comments

Does this seem correct?: String info = String.format("USI" + info.replace("USI",""),"USI%sCN%s,WN%s", tag + status + tag + mdn +tag + DToDevice + tag, mobileStr, totalStr + settings.getString("last_month", "0"));
No, it looks like you're defining info recursively. I meant, define info as you already are, and then set info = "USI" + info.replace("USI",""); as a second statement.
One more quick (related) question - if I want to replace the 2nd CN with CO - how can I do so using the method you suggested - I tried info = "CN" + info.replace("CN","CO"); but it replaced both CN's with CO (and I only want to replace the 2nd one)
1

To replace the first USI that isn't at the start of the string:

String info = String.format(...);
info = info.replaceFirst("(?<=.)USI","");

To replace the second USI:

String info = String.format(...);
info = info.replaceFirst("(?<=USI.{0,10000})USI","");

(or another solution that may also suit your needs)

Test.

You can similarly use replaceAll to replace all occurrences.

The above makes use of regular expressions.

(?<=...) means the characters before must match the given pattern, which is, in the first case, anything / a wild-card (.). In the second case, we look backwards until we find a USI. The .{0,10000} means 0-10000 wild-cards (.*, which means zero-or-more wild-cards, doesn't work because look-behind needs a max length).

See this for more on Java regex.

4 Comments

Using this method causes my application to crash with the following error: Look-behind pattern matches must have a bounded maximum length near index 18:
The code works for me. Make sure you copied it exactly as is. You can paste a few lines of code reproducing the error here and give me a link to it if you can't get it to work.
Try my first suggestion ((?<=.)) (if it suits your needs) or any of these.
I have a related question... mind taking a look? stackoverflow.com/questions/17305105/…
0

You could, as a workaround, find the position of the latter USI with String.lastIndexOf and then work with String.substring using that position to split it into two parts without the second USI.

2 Comments

Can you show me an example? I've never used this method before (I'm a bit new to the game if you cant already tell) ; )
I have another (related) question... mind taking a look? stackoverflow.com/questions/17305105/…

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.