I am developing an API as a bit of a student hobby-project, and I'm using a url where different options can be added/subtracted to change the information received. I know strings are immutable, which has made things a bit challenging to work around neatly. I've come up with a few a messy ways, none that robust, to solve it, and I'm wondering if there is a standard approach. I plan to have radio buttons (not assigned to a button a group; want all permutations) that will allow me to "include", "add", and "exclude" options.

Thus, here is the string z?s=GOOG&t=7m&z=l&q=l&p=e50,m50&a=ss,sfs,vm,w14 used to interact with YahooFinance API. So, assume that I have some variable like this:

String options="z?s=GOOG&t=7m&z=l&q=l&p=e50,m50&a=ss,sfs,vm,w14"

Now, consider the part that says p=e50,m50. Suppose I have three radiobuttons 50,100,200 that I want to tick on/off to such that I'd have a string p=e50,m50,e100,m100,e200,m200 with all buttons on, and every possible other combination; i.e. with 100 off, it would look like: p=e50,m50,e200,m200.

Any thoughts? I'd like to ultimately do the same with the other value (ss,sfs, etc.) as well, however, let's just start with the moving averages.

Have you considered BitArray for on/off representation of your buttons? You can construct the final string on the fly when you need it?
–
PM 77-1Mar 29 '13 at 20:42

Just build the string you want. You can just concatenate short strings, or you can get fancy with a char array or StringBuilder or some sort of formatter. Not rocket science.
–
Hot LicksMar 29 '13 at 22:14

(Hint: Read the spec for the String class to see what functions are available there. From there, if you wish, you can go to StringBuilder.)
–
Hot LicksMar 29 '13 at 22:15

6 Answers
6

Although everyone else is right that this isn't the right approach, it IS the one he asked for :)
–
Bill KMar 29 '13 at 21:01

@BillK, don't think so, since insert() requires an offset, and this answer doesn't say how to get one. Also, OP asks to remove bits, too. So, no, this is even the wrong answer to a wrong question :P
–
RaffaeleMar 29 '13 at 21:05

StringBuffer would be more accurate since it's really just a mutable string, but I figured this was close enough.
–
Bill KMar 29 '13 at 23:59

Strings are only immutable in the sense that there is no way to change the contents of a particular instance of a string once it's created in the JVM. That said, that doesn't mean you can't construct strings on the fly that suit your needs. You could use simple concatenation and do something like...

Technically, this creates three String instances, and a StringBuffer is used to append them all together. You could use the StringBuffer API yourself and call .append() in the places you want to add options.

Using mutable strings would just lead to messy code. What you need is a data structure where it's easy to update the parameters, then traverse that data structure to create a string from it. I'd store these parameters in a map and write a function that would create the string from the map by iterating through the map entries.

There's a lot of other options and details for using format strings, but that's the basic idea. The good part is that the unchanging part of the structure of your string is clear, and you only replace the parts that need replacing.

It'll also teach you how to use C and C++'s printf, which is never a bad thing :)

Why do you want to mutate a string? And why do you approach this problem from the String perspective? This question is not about text. What this sequence of characters

?s=GOOG&t=7m&z=l&q=l&p=e50,m50&a=ss,sfs,vm,w14

represents it's just a map (also called a dictionary), ie a collection of key-value pairs, serialized according to the URI encoding scheme and appended to the URL of a HTTP request. You don't want to keep track of the serialized parts, especially because a URI accepts only a small subset of characters, so if you reason in terms of strings, you'll likely end up with non-working code.

You need two methods: one to serialize a Map to the URI format, and one to get back a Map from its serialized form. Note that the Yahoo protocol uses a comma to assign multiple values to the same key. You have to account for this, too, when serializing and deserializing.

I wrote a gist for you. It's not thoroughly tested, but you get the idea.