6 Replies - 737 Views - Last Post: 22 March 2010 - 08:26 PM Rate Topic: -----

#1 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 13-March 10

Searching for comments blocks within a String

Posted 22 March 2010 - 07:47 PM

I have an assignment where I type in a string and search for the comment blocks

Quote

/* */
and I have to remove them if they exist in the string

here is what I have so far for my method I need to create:
 public static String remove(String s)
	{
		int one=s.indexOf("/*");
		int two=s.lastIndexOf("*/");
		String ss=null;
		if(one==0)
			ss=s.substring(two+1);
		else if(one >0)
			ss=s.substring(0,one)+s.substring((two+1));
		else 
			ss=s;
		return ss;

My input is:

Quote

ind any comment blocks: hi /*chicken*/ pie
The new string is: hi / pie
so my problem is the / I am not sure exactly how to get rid of it... I tried to do
int one=indexOf("//*"); 
and it kept the commented block

any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Searching for comments blocks within a String

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Searching for comments blocks within a String

Posted 22 March 2010 - 07:56 PM

In order to do something like this, you should use the two-argument indexOf() method. It takes a search string and an index with which to begin looking. You should also put this in a for loop so you can remove ALL instances of it. NOTE: indexOf returns -1 when it cannot be found.
public static String remove (String s) {
    int start = 0; 
    int end = 0;

    while (true) {
        start = s.indexOf("/*");
        end = s.indexOf("*/", start); // Look after start
 
        if (start == -1 || end == -1)
            break;

        String first = s.substring(0, start);
        String second = s.substring(end+1);
        s = first + second;
    }
 
    return s;
}



Please note that my compiler is broken atm and i have not tested this yet.
Was This Post Helpful? 0
  • +
  • -

#3 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 13-March 10

Re: Searching for comments blocks within a String

Posted 22 March 2010 - 08:01 PM

sorry, the only thing i forgot to mention was that I can't use iterations =(
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Searching for comments blocks within a String

Posted 22 March 2010 - 08:07 PM

No loops? Surely they only expect you to remove one comment block then, as that is the only way I can think of doing it the moment...Let me do some looking.
Was This Post Helpful? 0
  • +
  • -

#5 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 13-March 10

Re: Searching for comments blocks within a String

Posted 22 March 2010 - 08:13 PM

yeah I have to remove the comment block, here are the directions:

Quote

Write a method that tries to find opening and closing comment marks "/*" and "*/", in a string. If both are found, the method removes the first opening mark, the last closing mark, and all of the characters between them from the string and returns the new string. If one or both marks are not found, the method returns the original string unchanged. Your method must rely on String's methods and not use any iterations explicitly


That is from the sheet i received for the assignment.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Searching for comments blocks within a String

Posted 22 March 2010 - 08:22 PM

in that case, you were on the right track:
 public static String remove(String s)
        {
                // Get indexes
                int one=s.indexOf("/*");
                int two=s.lastIndexOf("*/");
                
                // If one isn't found, just return the string given
                if (one == -1 || two == -1)
                    return s; 

                String ss = "";
                if(one==0)
                        ss=s.substring(two+2);
                else if(one >0)
                        ss=s.substring(0,one)+s.substring((two+2));
                else 
                        ss=s;
                return ss;
      }



As you can see, I changed the +1's to +2's because you search for "*/",and +1 merely skips the *. +2 skips both the * and the "/"
Was This Post Helpful? 1
  • +
  • -

#7 Parrish  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 13-March 10

Re: Searching for comments blocks within a String

Posted 22 March 2010 - 08:26 PM

ah, alright, thanks! that definitely helps me out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1