pine-scriptpine-script-v5pine-script-v4

How to extract strings in from an array of strings using Pinescript?


How to remove certain strings from a source array to create a new array? I have tried str.split() but not working.

Current table title look like this from tickerid: NSE:CNXAUTO, NSE:BANKNIFTY, NSE:CNXENERGY

Expected table title: AUTO, BANK, ENERGY

I need to remove: 'NSE:', 'CNX', 'NIFTY' from the array

Also, how to createa a boolean with i_show_tickerid input?

Code:

i_show_tickerid = false

si_title_arr = array.from('NSE:CNXAUTO', 'NSE:BANKNIFTY', 'NSE:CNXENERGY')

if barstate.islast
    for i = 0 to array.size(si_title_arr) - 1
        text_i = array.get(si_title_arr, i)
        transp_i = array.get(transp_arr, i)
        table.cell(table_id=tc, column=i + 1, row=2, text=text_i, text_color=#808080, bgcolor=color.new(#808080, transp_i))

Solution

  • [SOLVED] Remove strings from an array of strings:

    Code:

    //@version=5
    indicator('str_clean')
    
    // Function to clean strings
    str_clean(source_array, remove_array) =>
        new_array = array.new_string(0)
        for i = 0 to array.size(source_array)-1
            s = array.get(source_array, i)
            for j = 0 to array.size(remove_array)-1
                s := str.replace_all(s, array.get(remove_array, j), "")
            array.push(new_array, s)
        new_array
    
    // Execute Function
    ticker_array = array.from("NSE:CNXAUTO", "NSE:BANKNIFTY", "NSE:CNXENERGY")
    remove_strings = array.from("NSE:", "CNX", "NIFTY")
    new_array = str_clean(ticker_array, remove_strings)
    
    // Table
    var table tc = table.new(position=position.top_right, columns=4, rows=2)
    
    for i = 0 to array.size(new_array) - 1
        text_i = array.get(new_array, i)
        table.cell(table_id=tc, column=i + 1, row=1, text=text_i, text_color=#808080, bgcolor=color.new(#808080, 80))