Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for substring #80

Closed
drewburlingame opened this issue Jun 23, 2016 · 9 comments
Closed

support for substring #80

drewburlingame opened this issue Jun 23, 2016 · 9 comments
Labels

Comments

@drewburlingame
Copy link

i can see that ToLower and ToUpper are both supported string extensions, but didn't see support for Substring. Is Substring supported and I'm missing it? If not, do you think it would be trivial to support?

Thanks,
Drew

@axunonb
Copy link
Member

axunonb commented Nov 1, 2016

You can only use methods without parameters. Implementing parameters is not that trivial.

@axunonb axunonb closed this as completed Nov 1, 2016
@arilani
Copy link

arilani commented Sep 6, 2017

I've created a substring extension:
`using System;
using System.Collections.Generic;
using SmartFormat.Core.Extensions;
using SmartFormat.Core.Parsing;

namespace SmartFormat.Extensions
{
public class SubstringFormatter : IFormatter
{
public string[] Names { get; set; } = { "substring", "substr", "s", "mid" };

    private char _parametersSepChar = ',';
    public char ParametersSepChar { get { return _parametersSepChar;  } set { _parametersSepChar = value; } }

    public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
    {
        if (formattingInfo.FormatterOptions == "") return false;
        string[] parameters = formattingInfo.FormatterOptions.Split(_parametersSepChar);
        if (parameters.Length < 1) return false;

        object currentValue = formattingInfo.CurrentValue;
        string currentValueString = (currentValue == null) ? "null" : currentValue.ToString();

        int startPos = int.Parse(parameters[0]);
        int length = parameters.Length > 1 ? int.Parse(parameters[1]) : 0;
        if (startPos < 0)
            startPos = currentValueString.Length + startPos;
        if (startPos > currentValueString.Length)
            startPos = currentValueString.Length;
        if (length < 0)
            length = currentValueString.Length - startPos + length;
        if (startPos + length > currentValueString.Length)
            length = 0;
        string substring = parameters.Length > 1 ? currentValueString.Substring(startPos, length) : currentValueString.Substring(startPos);

        formattingInfo.Write(substring);

        return true;
    }
}

}`

Here you can found some examples:
Example: #1: {Person.Name} starts with {Person.Name:s(0,3)} #2: {Person.Name} ends with {Person.Name:substr(-3)} #3: {Person.Name} ends with {Person.Name:substr(5)} #4: {Person.Name} contains {Person.Name:substring(2,-2)} #5: {Person.Name} not contains {Person.Name:mid(20,10)}

@axunonb
Copy link
Member

axunonb commented Sep 18, 2017

Hi, that's very good, thank you!
I plan to add section for contributed extensions in the repo and I'll add yours as soon as time allows.

@axunonb axunonb reopened this Sep 18, 2017
@axunonb
Copy link
Member

axunonb commented Oct 18, 2017

Do you think your request could be covered with feature request #88 (using RegEx). RegEx will bring about 50% performance penalty, though.

@scottrippey
Copy link
Contributor

I really like this!
I think substr is a very common use-case, and this extension proves it's pretty simple, so I think it would make sense to include it in the core.
Just my 2¢ ;)

@arilani
Copy link

arilani commented Jun 26, 2018

Thanks!

@mweaver026
Copy link

Is there any plan on when another release would be created and pushed to Nuget with this feature included?

@axunonb
Copy link
Member

axunonb commented Nov 15, 2018

Release 2.3.1.0 is published, covering this feature.

@mweaver026
Copy link

Thank you! I appreciate the quick response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants