Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlucardJay · Jul 27, 2012 at 06:58 AM · javascriptstringparser

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 )

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jul 27, 2012 at 10:30 AM 0
Share

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.

avatar image AlucardJay · Jul 27, 2012 at 12:44 PM 0
Share

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 =]

avatar image AlucardJay · Jul 27, 2012 at 01:38 PM 0
Share

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, "

avatar image AlucardJay · Jul 27, 2012 at 01:41 PM 0
Share

How do I denote removing a tab with String.Replace(" TAB ", ""); ?

avatar image AlucardJay · Jul 27, 2012 at 04:21 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jul 27, 2012 at 12:19 PM 0
Share

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 );
 }
avatar image
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.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jul 27, 2012 at 10:12 AM 0
Share

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

avatar image AlucardJay · Jul 27, 2012 at 11:04 AM 0
Share

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....

avatar image whydoidoit · Jul 27, 2012 at 11:06 AM 1
Share

I believe it should be:

   _word1 = _word1.Trim("\"".ToCharArray());
avatar image whydoidoit · Jul 27, 2012 at 11:08 AM 1
Share

Or to remove them anywhere:

     _word1 = _word1.Replace("\"", "");
avatar image flamy · Jul 27, 2012 at 11:09 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges