Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

splitting binary string into several two character?

 
Sara Jonasn
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all

I have a binary string like "100010" . I want split it into several two character like -> "10" ,"00","10".

How can i do it? Unfortunately i have no idea about it

please help me,
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sara Jonasn wrote:HI all

I have a binary string like "100010" . I want split it into several two character like -> "10" ,"00","10".

How can i do it? Unfortunately i have no idea about it

please help me,


In Jsvs, strings are objects -- specifically, instances of the java.lang.String class. And this class has a ton of methods that can do a ton of stuff. For example, one possible option is to use the substring() method.

Henry
 
Sara Jonasn
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that i can use substring() but i don't know how it can help me!!!
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sara Jonasn wrote:I know that i can use substring() but i don't know how it can help me!!!


The substring() mthod takes two indexes -- the begin index and the (exclusive) end index. So, to get the first substring, the indexes are zero and two; To get the second, use two and four; four and six; six and eight; etc. Can you figure out how to do that with a loop?

Henry
 
Sara Jonasn
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already think to your suggested idea but i could not find any algorithm or loop.
I writed this code;
String str = "100010";
int counter=0,end=1;

for (int h=0;h<str.length();h++){

String ss = str.substring(counter, end);
System.out.print(ss);

counter= counter+2;
end=end+2;

}
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sara Jonasn wrote:I already think to your suggested idea but i could not find any algorithm or loop.
I writed this code;



Well, you pretty much got the jist of it done. You just need to get the loop counters correct. To do that, I recommend that you work it out on paper -- pretend that you are computer, and work through it.

Henry
 
Sara Jonasn
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think on it enough but i could not solve it so i posted to this forum. I don' have enough time to think again therefor if you know it tell me please .

Many Thanksss,
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sara Jonasn wrote:I think on it enough but i could not solve it so i posted to this forum. I don' have enough time to think again therefor if you know it tell me please .


Sorry, but coderanch is a learning site. The goal here is for you to learn. You tell the issue, and we give hints in the correct direction. In the end, we get to the solution together, but you will have to do most of the heavy lifting.

Henry
 
Sara Jonasn
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,thanks for your help
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sara Jonasn wrote:Ok,thanks for your help


If you still want to give it another try ... remember from my previous hint that the loop needs to have the indexes (begin, end) go like ... (0,2) (2,4) (4,6) ... (n, n+2) ... (stringlen-2, stringlen). Can you figure out how to make the loop indexes do that?

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic