0

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

ThisDude
  • 111
  • 2
  • 9
  • Does your question actually have anything to do with your C# code? It looks like you're trying to do everything in your Java code, right? It's very unclear what the part to do with IDs is meant to do, but that sounds separate from the splitting part anyway... – Jon Skeet Sep 15 '15 at 13:17
  • ReceivedPairs[i] = ReceivedPairs[i].Split("/")[0] + ReceivedPairs[i].Split("/")[1] – kevintjuh93 Sep 15 '15 at 13:18
  • I am doing it in my Java Code... Nothing deals with C#, Only that they both handle the ID. So, It must be the Same ID they see.... Thanks for your kind answer.. – ThisDude Sep 15 '15 at 13:20
  • Do you want to transform the first one to the second one? Check out substrings: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int) – kevintjuh93 Sep 15 '15 at 13:28
  • I see that now, thanks.. But, how will i Split the arrays into this SubString.. I'm new at Java.. – ThisDude Sep 15 '15 at 13:32

1 Answers1

1

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);
João Neves
  • 944
  • 1
  • 13
  • 18