C# String treatment

  • Hello,

    I have a line of code like: Filenameonly2 = line

    I get a result like:

    When I do messagebox.show  ( Filenameonly2 ) I get:

    16 DIVIDENDS SAME DAY                D_5011R.08                     20171128

    But I would like to have this as:

    16_DIVIDENDS_SAME_DAY_D_5011R.08_20171128

    I have tried with:

    Filenameonly2 = line.replace(" ","_") but instead of getting the above I get:

    16_DIVIDENDS_SAME_DAY_______________D_5011R.08______________20171128

    Can you please help?

  • river1 - Monday, December 18, 2017 7:27 AM

    Hello,

    I have a line of code like: Filenameonly2 = line

    I get a result like:

    When I do messagebox.show  ( Filenameonly2 ) I get:

    16 DIVIDENDS SAME DAY                D_5011R.08                     20171128

    But I would like to have this as:

    16_DIVIDENDS_SAME_DAY_D_5011R.08_20171128

    I have tried with:

    Filenameonly2 = line.replace(" ","_") but instead of getting the above I get:

    16_DIVIDENDS_SAME_DAY_______________D_5011R.08______________20171128

    Can you please help?

    Step 1: replace multiple concurrent spaces with a single space.
    Step 2: replace single spaces with an underscore.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin - Monday, December 18, 2017 7:32 AM

    river1 - Monday, December 18, 2017 7:27 AM

    Hello,

    I have a line of code like: Filenameonly2 = line

    I get a result like:

    When I do messagebox.show  ( Filenameonly2 ) I get:

    16 DIVIDENDS SAME DAY                D_5011R.08                     20171128

    But I would like to have this as:

    16_DIVIDENDS_SAME_DAY_D_5011R.08_20171128

    I have tried with:

    Filenameonly2 = line.replace(" ","_") but instead of getting the above I get:

    16_DIVIDENDS_SAME_DAY_______________D_5011R.08______________20171128

    Can you please help?

    Step 1: replace multiple concurrent spaces with a single space.
    Step 2: replace single spaces with an underscore.

    Unfortunately it seems not to work.

    I have done line:

    Filenameonly2 = (line.Replace(" ", " "));

    Filenameonly2 = (line.Replace(" ", "_"));

    //Filenameonly2 = ((line.TrimStart().Replace(" ", "_")).Replace("#", ""));

    MessageBox.Show(Filenameonly2);

    I still get the same:

    16_DIVIDENDS_SAME_DAY_______________D_5011R.08______________20171128

  • Sorry. My bad:

    Filenameonly2 = (line.Replace(" ", " "));

    Filenameonly2 = (Filenameonly2.Replace(" ", "_"));

    But it still not works...

    I still get

    16_DIVIDENDS_SAME_DAY_______D_5011R.08_______20171128

    Initially I was getting

    16_DIVIDENDS_SAME_DAY________________________D_5011R.08_____________________20171128

    Now I am getting:

    16_DIVIDENDS_SAME_DAY_______D_5011R.08_______20171128

  • river1 - Monday, December 18, 2017 7:46 AM

    Sorry. My bad:

    Filenameonly2 = (line.Replace(" ", " "));

    Filenameonly2 = (Filenameonly2.Replace(" ", "_"));

    But it still not works...

    I still get

    16_DIVIDENDS_SAME_DAY_______D_5011R.08_______20171128

    Initially I was getting

    16_DIVIDENDS_SAME_DAY________________________D_5011R.08_____________________20171128

    Now I am getting:

    16_DIVIDENDS_SAME_DAY_______D_5011R.08_______20171128

    Your logic for replacing multiple spaces is flawed. Take a read here, for example.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin - Monday, December 18, 2017 7:51 AM

    river1 - Monday, December 18, 2017 7:46 AM

    Sorry. My bad:

    Filenameonly2 = (line.Replace(" ", " "));

    Filenameonly2 = (Filenameonly2.Replace(" ", "_"));

    But it still not works...

    I still get

    16_DIVIDENDS_SAME_DAY_______D_5011R.08_______20171128

    Initially I was getting

    16_DIVIDENDS_SAME_DAY________________________D_5011R.08_____________________20171128

    Now I am getting:

    16_DIVIDENDS_SAME_DAY_______D_5011R.08_______20171128

    Your logic for replacing multiple spaces is flawed. Take a read here, for example.

    I have changed to this:

    Filenameonly2 = line.Replace(" ", "()").Replace(")(", "").Replace("()", " ");

    Filenameonly2 = Filenameonly2.Replace(" ", "_");

    Filenameonly2 = Filenameonly2.Replace("#_", "");

    It is working now.

  • Now I have another challange. Maybe you can also help me with this.

    After I get that line (which I use to set as the new file name , I move into the next lines. The next lines will be stored inside a excel file as columns.

    They are as following:

    #  1 Calculation Date                  calc_date                      D   8  0
    #  2 As Of Date                        as_of_date                     D   8  0
    #  3 MSCI Index Code                   msci_index_code                N   6  0
    #  4 Index Variant Type                index_variant_type             S   4  0
    #  5 MSCI Index Name                   index_name                     S  40  0
    #  6 Last Rebalancing Date             last_rebalancing_date          D   8  0
    #  7 Next Rebalancing Date             next_rebalancing_date          D   8  0
    #  8 Number of Securities in Index     nb_of_securities               N   6  0
    #  9 Number of Issuers in Index        nb_of_issuers                  N   6  0
    # 10 Number of Group Entities in Index nb_of_group_entities           N   6  0
    # 11 Index Variant Base Date           base_date                      D   8  0
    # 12 Index Variant Base Value          base_value                     N  19  6
    # 13 Initial Mkt Cap in MM USD         initial_mkt_cap_eod00d_usd     N  17  6
    # 14 Adjusted Mkt Cap in MM USD        adj_mkt_cap_eod00d_usd         N  17  6
    # 15 Closing Mkt Cap in MM USD         unadj_mkt_cap_eod00d_usd       N  17  6
    # 16 Index level in Local (today)      eod00d_local                   N  19  6
    # 17 Index Level in Local (prev day)   eod01d_local                   N  19  6
    # 18 Daily perf in Local (pct)         perf_eod01d_local              N  10  3
    # 19 Base Currency ISO Symbol          ISO_symbol_ccy0                S   3  0
    # 20 Index level in Base Curr (today)  eod00d_ccy0                    N  19  6
    # 21 Index Level in Base Curr (prev)   eod01d_ccy0                    N  19  6
    # 22 Daily perf in Base Curr (pct)     perf_eod01d_ccy0               N  10  3
    # 23 Currency 1 ISO Symbol             ISO_symbol_ccy1                S   3  0
    # 24 Index level in Curr 1 (today)     eod00d_ccy1                    N  19  6
    # 25 Index level in Curr 1 (prev day)  eod01d_ccy1                    N  19  6
    # 26 Daily performance in Curr 1 (pct) perf_eod01d_ccy1               N  10  3
    # 27 Currency 2 ISO Symbol             ISO_symbol_ccy2                S   3  0
    # 28 Index level in Curr 2 (today)     eod00d_ccy2                    N  19  6
    # 29 Index level in Curr 2 (prev day)  eod01d_ccy2                    N  19  6
    # 30 Daily performance in Curr 2 (pct) perf_eod01d_ccy2               N  10  3
    # 31 Estimated Daily Yield             daily_yield                    N  19  3
    # 32 Hedging Currency ISO Symbol       hedging_currency_ISO_symbol    S   3  0
    # 33 Percent Hedged                    percent_hedged                 N   7  3
    # 34 Date of Fundamental Ratios        ratios_data_date               D   8  0
    # 35 P/E                               price_to_earnings              N  10  3
    # 36 P/CE                              price_to_cash_earnings         N  10  3
    # 37 P/BV                              price_to_book_value            N  10  3
    # 38 Yield                             yield                          N  10  3
    # 39 ROE                               roe                            N  10  3
    # 40 Price to 12 months Fwd Earnings   fwd_price_to_earn              N  10  3
    # 41 Long Term EPS Growth Trend        lt_hist_eps_g                  N  10  3
    # 42 Long Term SPS Growth Trend        lt_hist_sps_g                  N  10  3
    # 43 Long Term EPS Growth Trend (3yrs) lt_hist_eps_g_3                N  10  3
    # 44 Long Term SPS Growth Trend (3yrs) lt_hist_sps_g_3                N  10  3
    # 45 Long Term Fwd EPS growth rate     lt_fwd_earn_per_share_g        N  10  3
    # 46 Short Term Fwd EPS growth rate    st_fwd_earn_per_share_g        N  10  3
    # 47 12 months trailing Index EPS      12m_trailing_eps               N  10  3
    # 48 12 months forward Index EPS       12m_forward_eps                N  10  3
    # 49 Year on year growth trailing EPS  y_on_y_growth_trailing_eps     N  10  3
    # 50 Year on year growth forward EPS   y_on_y_growth_forward_eps      N  10  3
    # 51 Current Internal Growth rate      curr_internal_growth_rate      N  10  3
    # 52 1 Year ADPS Growth Rate           1_year_ADPS_growth_rate        N  10  3
    # 53 5 Years ADPS Growth Rate          5_years_ADPS_growth_rate       N  10  3
    # 54 Payout Ratio                      payout_ratio                   N  10  3

    What I want is transfor this (pivot) into column names that I will latelly store in the excel file.

    Each of the above is a column, but only the word in the middle. For example:

    # 54 Payout Ratio                      payout_ratio                   N  10  3       --> Column name is payout_ratio                  
    or (as another example:
    #  1 Calculation Date                  calc_date                      D   8  0  --> Column name is  calc_date                     

    How can I get from every line just the middle word and then pivot to store it as column names ?

  • I think the way is to say that column is equal to subctring 39 to 70 (and replace white spaces) . This way we always catch the midle word

  • Well, I can think of three ways off the top of my head, assuming I understand what you're asking for...

    Assuming the field name always contains a "_" (and is the only part of the string that does)...
    1) Find the position of the space prior to the first "_" in the string
    2) Find the position of the space following the last "_" in the string
    3) Take substring starting at [first space + 1] to [last space -1]

    Or, if the list of expected field names is fixed and known ahead of time, have a "lookup table" containing the field name values you expect to find and search each string for each expected field name value. Loop through each search string and for each loop through your field names, seeing if the expected field name value is contained in the string until you either find the field name in one of the search strings, or you don't. Won't work if your field names change though, unless you know ahead of times what the expected field names will be.

    Or, for each string value, split the string on spaces (into an array/list), and then (again, if field name is the only one with a "_" character), search each piece array until you find the string in the array with a "_", and that's your field name.

    None of these is terribly efficient but they are pretty easy to code up and if your data are small then it should be fine.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply