How do I remove the leading zeros in a string? (000010)

0 favourites
From the Asset Store
Creepy Sprite Font suitable font for your Dark/Horror/Halloween games.
  • Is there any way I can remove leading zeros in string variables?

    Examples: 010, 00000001, 00000100 should become 10, 1, 100.

    This variable needs to stay a string, I can't convert them to int() or I risk rounding errors on longer numbers. I know how to use mid() to compare the first digit and how to use while to loop. I also know how to use Replace(), but I don't know how to control that Replace() to only the 1st digit. I suspect a few of the app users may copy/paste so monitoring input may cause problems. I mostly just need an expression, action or the basic logic behind it so I can remove the leading zeros all together or one at a time and I can just loop.

    ====edit====

    After some thought I suppose I could compare each digit consecutively using mid() and a loopindex(). If the selected digit ever gets above int(0) then move the rest of the digits to a new string. I feel there may be a better way though.

    ====edit2====

    I understand user input would have to be in something that isn't a variable. I would assume the process would be the same though. Right now I only have this working in debug, I am keeping it modular so I can copy/paste it for later uses.

    ====edit3====

    Solution is in this C3P. This C3P has some examples of a few specific things that not many people will need. Be sure to Disable all groups expect for the one with Regex() in the Group name.

    dropbox.com/s/1v415k8h85jp2wa/Tutorials.c3p

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • RegexReplace(Self.Text, "^0+", "", "")

    You should look into the RegexReplace function. The code written above is what you set the text to in the set text action.

    There is no need for a loop. No reason to check the string 1 character at a time. Regex simply looks for a sequence of characters that match the search criteria entered in the second parameter and replaces it with the value in the forth parameter. Of course you should also learn about regex but basically this is what it is looking for:

    ^ - This signifies that is starts at the beginning of the string. If there is a match further in the string, it won't be replaced because the match must be the first thing in the text.

    0 - This is the character the regex is looking to match.

    + - The plus sign following a number or letter says to match 1 or more instances of that character. So, in the case of leading 0s, it doesn't matter if there is 1 or 100, they match and will be replaced.

    Regex is an extremely powerful way to work with and manipulate text objects and can save both coding and processing time.

  • I don't have to know how many leading Zeros there are? It will remove them until there isn't a leading zero?

  • That's correct.

  • Just copy that first line of my post and paste it right into your set text action and watch the magic happen.

  • Best thing I have heard in a while.

    I already have loops in loops.

    I don't want more loops in loops in loops.

    Thanks.

  • No problem. Make sure to take some time to read about regex. It is a common programming tool that will serve you well. And, good luck with your project.

  • I have it working and was going to upload my tutorial to the original post. It has many kinds of random examples in it. I gave you credit using your username. I can remove it if you want. I want to type more out about Regex() in comments before I post it though. I want to learn about RegexReplace(String, Regex, Flags, Replace)

    Do you have a link that I can learn about Regex? I understand String, Flags and Replace. I am not sure how to search for (Regex). I keep finding tutorials but I don't see anything called Regex other than the expression name. Does it go by different names? What goes there? I just need to be pointed in a direction for now to find out about the Regex that is in parenthesis.

  • regexr.com

    That site has refernce sheets to the different building blocks of a regex expression. It also allows you to build a regex expression and test it agains a line of text to see if you are getting the results you want.

    You can also just search regex on google and find lots of sites that teach about it. Not sure what tutorials you are finding or where you are looking. Don't search for regex tutorials related ton Construct. Regex is a broad subject used in programming in general. Most Construct tutorials will be too specific like the expression I posted that just removes leading 0s.

  • What I was looking at was accurate, it was just formatted differently so I didn't understand it at the time. It seems some people call (Regex) the Search Pattern or Pattern. For the most part I know how to use it now.

    Thanks

    I can get rid of some unneeded loops now on other events. It will increase my performance for sure.

  • I am sorry I need one last thing... I want to remove leading Zeros and all non-digits. Is there a way I can do this together or do I need to do this separately.

    I know RegexReplace(String, "^0+", "", "") will remove leading Zeros

    I know RegexReplace(String, "\D", "g", "") will remove all non-Digits

    After posting this I realize the g flag may mess up the ^0+....

  • (\D)*(^0+)*

    Try this pattern. The parens separate the 2 parts of the pattern and the * says search for 0 or more.

    You really shouldn't need any flags for this search.

    Remember, you can go to regexr.com to build and test regex patterns.

  • I have regexr.com saved I just don't fully understand how to use Regex on that site yet. I can only seem to add a search pattern and Global flag seems to always be on. I mostly didn't know that ( )( ) separated. That was mostly what I needed. Than you again.

    ===edit===

    While typing this I looked at the site again and I just figured it out. There is a dropdown that you have to use to select flags. Also I was expecting a Replace parameter.

    (\D)*(^0+)* /g is working on site and C3. The site is giving me a warning if g flag is on, but it won't work without it. "WARNING: The expression can return empty matches, and may match infinitely in some cases." Is this something I need to worry about, in C3?

  • I'm going to revise my previous post and say, you will want the global flag. When I wrote that, I was just thinking about leading 0s which will always come out as a single result. With the possibilty of non numeric characters throughout the string, You'll want the global flag to ensure you get all results. Also, I built you a more appropriate pattern.

    ((^[0\D]+)|(\D))

    The previous one would miss some leading 0s if a non-numberic character was in them (i.e. - 00g00100 would turn into 00100). Good luck with your project.

    Edit: changed the pattern to include an or.

  • I understand it on a basic level. I didn't think of a non-digit in the leading zero. I have to look up [] now, but I need to understand that anyway. It does give the same warning though.

    "WARNING: The expression can return empty matches, and may match infinitely in some cases."

    Will that effect my C3? I don't fully understand what would trigger it or it's full implications in C3. I have a feeling it is a conflict between * after the leading zero group and the global flag and probably doesn't really mean anything. I just want to confirm. I appreciate your help.

    *******edit************

    I tested it on about 10 regex test sites and most didn't give an error. The few that did all looked the same, as if they took the first sites code. I am more confident it is fine now and was just a false flag.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)