0

I am trying to do swa the bits but i have string which contains binary that is at first index[1] =1001 and at second index[2]=0010

I want TO swap there bits like

1001 and 0010 after bits swaps it becomes 1010 and 0001 and so on. 


for example
     A (array in string)
1.     1001
2.     0011
3.     1010
4.      0101
.
.
.    
now I want to swap the 1st two bits of 1 st string with 2nd string last 2 bits then 1st strings bit with 3rd so on
result :
     1011
     0001
     1000
     1011

I also applied this: String array[] = gf.split("\\", -1); but did not succeed

I just need to split the String into array. So just need help, thank you very much :)

user3508182
  • 457
  • 1
  • 4
  • 13

2 Answers2

0

If your Strings represent the binary data as String of {0,1} then to swap each character on all of your Strings in the index array, one of the greedy solutions would be to run replaceAll for all your Strings 3 times (one for temp)

for (String s: index)
    s = s.replaceAll("1","2").replaceAll("0","1").replaceAll("2","1");
Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24
0

Try the following for splitting your String:

String array[] = gf.split("(?!^)");

if you can work with Character[] you can simply do:

Character array[] = gf.toCharArray();
Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24