- Home /
Removing certain characters from strings and converting to a list
Hello!
I am sending query data from Unity3D to an external API. The API is taking my query, generating a result and sending it back.
However when it sends back certain values, it sends them back like this:
["c7385f31-2c3e-42ae-aae2-57c5ea66088f", "e6f8f4db-76c4-43a9-b5d3-5d3be606cabd", "22a55fa7-bf1f-46d9-9c1e-67336c4f55c4"]
That is a list of id codes sent from the API to me. However it is all one continuous string.
How would I go about converting this into a list?
I figure what I need to do is remove firstly the ['s and then seperate each one from each other. I'm not too sure how to go about it and have searched the internet for a solution but haven't found one.
(I am using C# btw, and heard that lists were better than arrays for this particular thing. Please do correct me if I'm wrong!)
Thanks!!
Answer by Berenger · May 20, 2012 at 05:04 AM
So, you receive "c7385f31-2c3e-42ae-aae2-57c5ea66088fe6f8f4db-76c4-43a9-b5d3-5d3be606cabd22a55fa7-bf1f-46d9-9c1e-67336c4f55c4" and you want to split it up in three and store each parts in a list am I right ?
I guess there isn't a character of separation then, but each part seems to have the same number of chars (I didn't count). You should use SubString and add each result to a List
I receive it exactly how I wrote it up there, brackets and commas and all.
Yes I think they are the same length (It would certainly make the job easier if they were!
I'm looking around for ways of getting rid of the ['s , and ,'s right now so that I will receive the result you posted
Thanks for the SubString Idea! Ill check it out :)
Thanks man!
Substring is perfect for what I needed! No need to individually cut any unwanted symbols out either! $$anonymous$$uch $$anonymous$$udos to you :)
EDIT: And just in case you're curious, each string is 346 chars long
Answer by Eric5h5 · May 20, 2012 at 05:42 AM
Just use String.Split, in combination with Substring to strip the outer characters. Then it doesn't matter if the lengths are the same or not. You don't need a List; an array is fine, which is what Split returns.
string input = "[\"c7385f31-2c3e-42ae-aae2-57c5ea66088f\", \"e6f8f4db-76c4-43a9-b5d3-5d3be606cabd\", \"22a55fa7-bf1f-46d9-9c1e-67336c4f55c4\"]";
string[] splitter = {"\", \""};
string[] idCodes = input.Substring(2, input.Length-4).Split(splitter, System.StringSplitOptions.None);
foreach (var id in idCodes) {
Debug.Log (id);
}
Thanks man! That certainly is an efficient way of doing it. $$anonymous$$uch appreciated!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Make Lists within a List 0 Answers
Confused about copying Lists! 1 Answer