Suneido

Integrated Application Platform

  • Home
  • Learning
    • Suneido Manual
    • Beginners
      • Inside Suneido
      • The Suneido Programming Language
      • The Suneido Database
      • Installing Suneido
      • Building Suneido
      • IDE Go To Tour
      • Upgrading To A New Release
    • Advanced
      • Canvas Control
      • DrawControl Part 1
      • DrawControl Part 2
      • DrawControl Part 3
      • SAX Like XML Processing
      • A Plug-In Architecture
      • A Simple Wizard Framework
      • An HTML Include Facility
      • An RSS 2 Feed Creator
      • MIME Generation
      • A New Add-on Facility
      • Workspace Improvement Hack
    • Mockito for Suneido
    • The Suneido Task Scheduler
    • Contributing To Suneido
    • Contributor Assignment of Copyright
    • Language Translation
    • Future Directions
    • Interview With Andrew Mckinlay
  • Forum
    • Announcements
    • Internals & Enhancements
    • Cookbook
    • General
  • FAQ
  • Screenshots
  • Downloads
  • Links

[SOLVED]A string composing function to share

Integrated Application Platform › Forums › General › [SOLVED]A string composing function to share

  • This topic has 3 replies, 2 voices, and was last updated 8 years, 5 months ago by amckinlay.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • August 18, 2014 at 1:58 am #782
    jaska_lee
    Participant

    I find it difficult to read and compose complex strings with $,”, space and substrings. So I came up with the another function to simplify the process.

    StringFormat

    function (@args)
    {
    str = args[0]
    subStrings = Object()
    for(i=1;i<=args.Size()-1;++i)
    {
    subStrings.Add(args)
    }

    j = 0
    for (s in subStrings)
    {
    ++j
    if (str.Find('%' $ String(j)) is str.Size())
    throw ('String format is invalid')
    str = str.Replace('%' $ String(j), s, 1)
    }

    return str
    }

    How to use it:

    str = StringFormat('yes, %1 %2 %3!', 'I', 'love', 'banana')
    Print(str)

    Result:

    yes, I love banana!

    The %n is an unlimited series of integer numbers starting from %1. The sub strings you can put in are also unlimited and can be any data type.
    Maybe the error handling can be improved a little but you can hardly miss it.

    Hope you find it useful.

    August 20, 2014 at 8:26 pm #1077
    amckinlay
    Keymaster

    Here’s an approach that uses the block form of Replace:

    args = #("%2 %1 %3", one, two, three)
    args[0].Replace("%[0-9]+", { args[Number(it[1..])] })

    This might also be a candidate for adding as a method in Strings so you could say: string.Format(args)

    August 21, 2014 at 1:25 am #1078
    jaska_lee
    Participant

    Wow! that’s really short and sweet. Thanks Andrew.
    Just one little suggestion, it would be nice to just supply the parameters as comma separated instead of an object, as it will save time for typing and reading later on when it is used.

    Cheers

    August 24, 2014 at 7:19 pm #1079
    amckinlay
    Keymaster

    When you use @args the arguments are put in an object for you, so you can call it with just regular arguments.

    function (@args)
    {
    return args[0].Replace(“%[0-9]+”, { args[Number(it[1..])] })
    }

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

Search Forums

Log In
Welcome to the new Suneido Web Site and Forum.
Register
Lost Password
users of the previous PHPBB Forum should be able to log in with their previous username and password.

Recent Topics

  • Alpha copy of gSuneido to play with
  • how to start jsuneido ?
  • Problem Starting Suneido…
  • Dialog not showing buttons
  • New link for Suneidojo

Contact Us | Legal Statement | Privacy Statement | SiteMap

Copyright © 2023 Axon® Development Corporation. All Rights Reserved. - Open Source Integrated Database and Programming Language