-3

I have a string that contains numbers like: 02101403101303101303140

how can I iterate the string to check whether the number in string is >= 2 and remember that number's index in array or list for further processing?

the further processing should be replacing substrings.

for example: the iterator found number 2 and remembers the index of this character. Now it takes the next character from 2 and remembers this character index also.

Now it is possible to replace the substring.

Let's say there is 21. Now I want this to become 11 Or lets say there is 60, this should be replaced with 000000. First number is indicator of "how many" and the second number is "what".

Or is there a better way to remember and replace certain substrings in that way?

Thank you in advance.

lkallas
  • 1,310
  • 5
  • 22
  • 36
  • 2
    Crate second empty string and parse your input char by char, if <2 insert this char to your output string, else, insert `char+1` number of times current char tells to. No code because it's highly probable homework. – zubergu Mar 23 '15 at 09:37
  • 2
    Your question is quite unspecific; it very much reads like "please do my homework for me". Please tell us where exactly you are stuck; or do you expect us to explain all the details of Java string processing to you? – GhostCat Mar 23 '15 at 09:38
  • Not expecting code. I would like to know which is the best approach for this operation. zubergu's approach is pretty good. So the storing indexes isn't a good idea. Creating the string on the fly is far better. So I'll go with that and post the code. – lkallas Mar 23 '15 at 09:49
  • This is compression algorithm attempt/excersise, isn't it? ;) – Gerino Mar 23 '15 at 09:51
  • Yep, you could say that ;) – lkallas Mar 23 '15 at 11:35

3 Answers3

0

There you go. but remember to atleast try next time

String str =  "02101403101303101303140";
StringBuilder sb = new StringBuilder();
for(int i=0; i < str.length(); i+=2)
  for(int j =0; j < Integer.parseInt(String.valueOf(str.charAt(i))); j++)
    sb.append(str.charAt(i+1));

System.out.print(sb.toString());
Rohit Rehan
  • 568
  • 1
  • 4
  • 18
0

Not sure if I'm understanding well your question, you could try something like this:

String mystring = "02101403101303101303140";
String target = "21";
String replacement = "11"
String newString = mystring.replace(target, replacement);
Carlos Borau
  • 1,433
  • 1
  • 24
  • 34
0
String str = "02101403101303101303140";

StringBuilder sb = new StringBuilder();
  for (int i = 0; i < str.length(); i++) {
      if(Integer.parseInt(String.valueOf(str.charAt(i))) >= 2) {
          int temp = Integer.parseInt(String.valueOf(str.charAt(i))) - 1;
          for (int j = 0; j < temp ; j++) {
              sb.append(str.charAt(i+1));
          }
      }
      else {
          sb.append(str.charAt(i));
      }
  }
  System.out.println(sb.toString());

This would produce: 01101000011101000111010001110000 which is binary for "http" (without quotes).

Thank you all! What I really needed was a push to right direction and thank zubergu for that. Also fr34k gave the best answer!

lkallas
  • 1,310
  • 5
  • 22
  • 36