我有一个我想要剥离并转换为另一个数组的数组

时间:2015-09-15 13:14:39

标签: java c# arrays

我有一个数组(2个字段数组),我想剥离" /"关闭第一个字段并检索剩余的数组具有相同的第二个字段值...

这就是我试图做的事情。

   CurrencyPairs = new string[] { "EURUSD", "USDJPY", "GBPUSD", "EURGBP", "USDCHF", "AUDNZD", "CADCHF", "CHFJPY", "EURAUD", "EURCAD", "EURJPY", "EURCHF", "USDCAD", "AUDUSD", "GBPJPY", "AUDCAD", "AUDCHF", "AUDJPY", "EURNOK", "GBPCAD", "GBPCHF", "NZDJPY", "NZDUSD", "EURNZD", "USDNOK", "USDSEK", "XAUUSD", "XAGUSD", "XTIUSD", "XBRUSD" };

此数组将从C#应用程序传递到我的Java应用程序,然后将为整个字符串创建一个整数ID ...

现在, 我想拆分第三个字符串" EUR" " USD"分成两个并添加" /"对它... 看起来像这样:

ReceivedPairs = new string[] { "EUR/USD", "USD/JPY", "GBP/USD", "EUR/GBP", "USD/CHF", "AUD/NZD", "CAD/CHF", "CHF/JPY", "EUR/AUD", "EUR/CAD", "EUR/JPY", "EUR/CHF", "USD/CAD", "AUD/USD", "GBP/JPY", "AUD/CAD", "AUD/CHF", "AUD/JPY", "EUR/NOK", "GBP/CAD", "GBP/CHF", "NZD/JPY", "NZD/USD", "EUR/NZD", "USD/NOK", "USD/SEK", "XAU/USD", "XAG/USD", "XTI/USD", "XBRUSD" };

并且,Java通过使它看起来像这样来向数组添加一个值..

func(String Pairs, int Id);
func("EURUSD",928);

哪一对将始终附加到生成的ID ..

如何将两个分成两个? 我如何在拆分后仍然使值不离开数组?

感谢你抽出时间..

编辑:我已经尝试了currencyPairs.strip,但是因为我没有价值在字符串中删除而无法工作......没有" / ",no" - "。只需按子串分割长度就是我需要的......我知道在C ++中,但在Java中...缺少

1 个答案:

答案 0 :(得分:1)

如果你正在使用Java 8,你可以这样做:

String[] CurrencyPairs = new String[] { "EURUSD", "USDJPY", "GBPUSD", "EURGBP", "USDCHF", "AUDNZD", "CADCHF", "CHFJPY", "EURAUD", "EURCAD", "EURJPY", "EURCHF", "USDCAD", "AUDUSD", "GBPJPY", "AUDCAD", "AUDCHF", "AUDJPY", "EURNOK", "GBPCAD", "GBPCHF", "NZDJPY", "NZDUSD", "EURNZD", "USDNOK", "USDSEK", "XAUUSD", "XAGUSD", "XTIUSD", "XBRUSD" };
String[] ReceivedPairs = Arrays.stream(CurrencyPairs)
    .map(s -> s.substring(0, s.length()/2) + "/" + s.substring(s.length()/2))
    .toArray(String[]::new);

如果您不想使用流,请使用简单的for循环:

ReceivedPairs = Arrays.copyOf(CurrencyPairs, CurrencyPairs.length);
for(int i = 0; i < ReceivedPairs.length; i++)
        ReceivedPairs[i] = ReceivedPairs[i].substring(0, ReceivedPairs[i].length()/2) + "/" + ReceivedPairs[i].substring(ReceivedPairs[i].length()/2);