2

I have an array of string values obtained from a method and I want to convert this array into a HTML readable format for getting/posting (eg. value=[12,21])

I have tried the following:

string[] array1 = methodToGetStringArray(); //assuming [12,21] for example
string finalString = "value="+array1; //intended output is value=[12,21]

Which of course doesn't work.

I would like to know the method to provided the value as shown above.

4
  • Thanks for all the answers. While all are actually correct, I think @choz 's answer is the cleanest answer of the few for simple arrays. Commented Dec 21, 2015 at 7:13
  • Side note: 12 is not really string... The fact that you are asking about string array makes most of the answers invalid as one can't simple add string as-is to JSON output. Commented Dec 21, 2015 at 7:45
  • @AlexeiLevenkov Now I got what you mean on my comment. Well, he expects for intended output is value=[12,21]. Commented Dec 21, 2015 at 7:52
  • I gave an honestly bad example for this question, but strings with proper formatting and validation (I handled that beforehand from an earlier method) was assumed to be done. And I have gotten it to work with strings (only alphanumerical). Haven't really managed to figure out a more appropriate rename for the question. Commented Dec 22, 2015 at 9:41

5 Answers 5

3

You can try,

string finalString = String.Format("value=[{0}]", string.Join(", ", array1));

finalString should return,

value=[12, 21]
Sign up to request clarification or add additional context in comments.

4 Comments

I considered to downvote, but apparently no one considers strings with quotes as valid option based on other answers... So let's just keep this questionable suggestion along with all other once here for OP to trip on. value=["a" b", "c",,,,,,, d"] :(
@AlexeiLevenkov I don't quite get what you mean. Are you saying that the format string should be value=['{0}'] which gives value=['12', '21']?
OP did not specify what types of values are in the string array (and 21 is not really string). So for ["a\" b", "c\",,,,,,, d"] your code will produce questionable JSON.
@AlexeiLevenkov Tbh, I don't know what the OP wants to use this code of. Yes, value is like an array of integer and this will mess up if he uses it as JSON.
3

Try like this:

string[] array1 = methodToGetStringArray();
string json = JsonConvert.SerializeObject(array1);

Refer JSON.NET

1 Comment

Hi, I am not using JSON.NET for my application but thanks for the input. I would use this method for other projects where I am allowed to use the library.
1

you can try this

string finalString = "Value = [" + string.Join(",", array1) + "]";

Comments

1

Use string.Join method:

string finalString = "value=[" + string.Join(",",array1) + "]";

Or JavaScriptSerializer:

var serializer = new JavaScriptSerializer();
var finalString = "value=" + serializer.Serialize(array1);

Comments

1
List<string> list = new List<string>(array1);
var a = "value=[" + list.Aggregate((x, y) => x + "," + y) + "]";    

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.