简体   繁体   中英

I have an Array which i Want to Strip and Convert to another array

I have an array(2 field array) which i want to Strip "/" off the first field and retrieve the remaining array With its same 2nd field value...

This is what i'm trying to do..

   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" };

This Array Will be passed from a C# App to my Java App Which Will then create a Integer ID for the whole Strings...

Now, I want to Split the third String "EUR" "USD" into two and add "/" to it... Which Will look like this:

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" };

And, the Java append a value to the Arrays by making it look this way..

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

Which One Pair Will always be attached to a Generated ID..

How do I Split the Pairs Into two? How do i Still make the values not leave the Array after the Splitting?

Thanks for your time taken..

EDIT: I have Tried currencyPairs.strip , but that won't work as i have no value to strip in the Strings... There is no "/", no "-". Just Splitting By Substring Length is what i need.. I know that in C++, but here in Java... Missing

If you're using Java 8, you can do something like this:

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);

Or a plain old for loop if you don't want to use streams:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM