- Home /
remove quotation marks from a parsed string
I am working on a texture packer .json file reader (I've been meaning to, and it came up in a question so I have finally started!), and the method I use in my .fnt file reader is not working in this project.
The difference is that in the font file, words are not surrounded by quotation marks "" , however the texture packer file does. How do I go about removing or reading between the quotation marks in my script?
function GetTextureInfo()
{
var _tempInfo : String[] = textureAtlasInfo.text.Split("\n"[0]);
var _tmp : String;
for (var input : String in _tempInfo)
{
var _words : String[] = input.Split(" "[0]);
for (var _word : String in _words)
{
var _words_split : String[] = _word.Split(":"[0]);
for (var _word1 : String in _words_split)
{
Debug.Log( "_word1 " + _word1 );
if ( String.Equals( _word1, "frame" ) )
{
Debug.Log("!! ** FOUND WORD => frame ** !!");
_tmp = _words_split[1].Substring( 0, _words_split[1].Length );
Debug.Log( "_tmp " + _tmp );
}
}
}
}
EDIT : this won't compile, but shows what kind of text I'm trying to break down :
var myParsedString : String = "{
"background.png":
{
"frame": {"x":2,"y":2,"w":480,"h":320},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":480,"h":320},
"sourceSize": {"w":480,"h":320}
},
"label.png":
{
"frame": {"x":2,"y":324,"w":202,"h":27},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":202,"h":27},
"sourceSize": {"w":202,"h":27}
}}";
function Start()
{
var _tempInfo : String[] = myParsedString.Split("\n"[0]);
var _tmp : String;
for (var input : String in _tempInfo)
{
var _words : String[] = input.Split(" "[0]);
for (var _word : String in _words)
{
var _words_split : String[] = _word.Split(":"[0]);
for (var _word1 : String in _words_split)
{
Debug.Log( "_word1 " + _word1 );
_word1 = _word1.Replace("\"", "");
Debug.Log( "_word1 " + _word1 );
if ( String.Equals( _word1, "frame" ) )
{
Debug.Log("!! ** FOUND WORD => frame ** !!");
}
}
}
}
}
EDIT 2 : as at _word1 = _word1.Replace("\"", "");
I've uploaded my full current script to compare/check : http://www.alucardj.net16.net/unityquestions/TexturePackerParser.js
and the text file : http://www.alucardj.net16.net/unityquestions/sampleText.js (its .js 'cos my host doesn't seem to like .txt )
Thanks for both answers. I was thinking about fafase's answer but something to remove the quotes would be better.
For example, the parsed text looks like this : {"background.png":{"frame": {"x":2,"y":2,"w":960,"h":640}
$$anonymous$$y string splitter first removes linebreaks and some punctuation, but as I am searching specifically for frame without quotation marks, it would be a longer process to Remove at for each part just to see if it said frame . But I am very grateful, it is useful information I shall remember.
I am about to check the Trim command now, and see if it works. Thanks again to you both for the great suggestions.
I have accepted the answer as a work-around, but would like to solve why the length for the word frame is returning 6 after splitting (and removing) the string with "/n" ":" and " " before this. Thanks for everyone that helped =]
After further testing I think it is a tab that is causing the problem
tempBulk = tempBulk.Replace("\r", "");
tempBulk = tempBulk.Replace("\n", "");
tempBulk = tempBulk.Replace("{", "");
tempBulk = tempBulk.Replace("}", "");
tempBulk = tempBulk.Replace(":", "");
tempBulk = tempBulk.Replace(" ", "");
Debug.Log( "tempBulk = " + tempBulk );
returns tempBulk = "frames""background.png" SPACE "frame""x"2,"y"2,"w"480,"h"320, SPACE "rotated"false, SPACE "trimmed"false, "
How do I denote removing a tab with String.Replace(" TAB ", ""); ?
For future readers, the answer to removing a tab with String.Replace(" TAB ", ""); is
String.Replace("\t", "");
http://answers.unity3d.com/questions/291545/remove-tab-from-parsed-string.html
Answer by fafase · Jul 27, 2012 at 10:07 AM
If you can grab the parameter you need:
var newString = "theParameter";
var index =newString.Length;
newString = newString.Remove(0,1);
newString = newString.Remove(index-1,1);
The first parameter of Remove is the index in the string we want to point and the second parameter is how many characters are to be removed. (I reckon -1 because Length is the null).
To be tried as I cannot but I guess that could help you.
http://msdn.microsoft.com/en-us/library/system.string.remove%28v=vs.71%29.aspx
This does also work fine, thanks (although the first remove made index shorter, so index-2 for the second statement). Using this method in the following code, I am getting the same problem as using Replace i.e the word frame is returning a length of 6
var newString = _word1;
var index = newString.Length;
if (index >= 2)
{
newString = newString.Remove( 0,1 );
newString = newString.Remove( index - 2, 1 );
}
Debug.Log( "_word1 " + _word1 + " : newString " + newString + " : " + _word1.Length + " : " + newString.Length );
so there is something existing before the word "frame" . but the split should have removed all the spaces before this.
After factoring in this observation and modifying the code, this works for me, so thanks =]
var newString = _word1;
var index = newString.Length;
if (index > 3)
{
newString = newString.Remove( 0, 2 );
newString = newString.Remove( index - 3, 1 );
}
Answer by flamy · Jul 27, 2012 at 10:10 AM
Did you try, String.Trim("\"".ToCharArray());
I was able to remove all the double quotes with this.
sorry if i am not getting the question.
I did not think of that one but I guess that is what he wanted...http://msdn.microsoft.com/en-us/library/system.string.trim%28v=vs.71%29
yep, the UDN change has totally stuffed formatting code , esp if you use '' to format code within a line e.g. if (foo > bar) if (foo > bar)
. It's interesting to note that the correct format comes through in the email ?!
anyway, I tested this and it didn't work. I have seen other notation while searching/reading and seen ' " ' used . is this possible (shall test anyway after writing this.
Here's what I just tried with Trim that didn't work :
for (var _word1 : String in _words_split)
{
Debug.Log( "_word1 " + _word1 );
//String.Trim(""".ToCharArray());
_word1.Trim(""".ToCharArray());
Debug.Log( "_word1 " + _word1 );
have I implemented it correctly ? (both debugs print the same thing i.e. "frame"
_word1.Trim('"'.ToCharArray());
also doesn't work....
I believe it should be:
_word1 = _word1.Trim("\"".ToCharArray());
Or to remove them anywhere:
_word1 = _word1.Replace("\"", "");
it is the same as whydoidiot's first comment, for some reason $$anonymous$$e gets changed to the format that is currently in the answer
Your answer
Follow this Question
Related Questions
Remove TAB from parsed string 2 Answers
script default variable value behaviour 2 Answers
Array of random loot items inside GUI.window 1 Answer
Combo Script Problem 1 Answer
Array of Array 1 Answer