aspose file tools*
The moose likes Beginning Java and the fly likes Looping StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looping StringBuffer" Watch "Looping StringBuffer" New topic
Author

Looping StringBuffer

N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
Hi Want loop through stringbuffer to check for indexof a string in the stringbuffer
i dont want to check string second time for the same position which i checked
previously how can i achieve this
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
Hi

When you get the StringBuffer and call the indexOf method, this will return an int value (representing the index of the sting you looked for). that you can store in a primitive variable reference.

This will be accurate for the indexOf on the StringBuffer, as long as the StringBuffer doesn't change.

HTH
Ror
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
if the stringbuffer changes then how we can achieve
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Read the API Documentation of StringBuffer, it includes an indexOf method that takes a "start at this index" parameter. If you start after the previously found occurrence, you'll find the next one.

(If you modify the string at that location, you know how you've modified it and how that may effect the indexing. (i.e. inserted "rabid" before "dog" - adjust the index appropriately, start searching again there))


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
if the StringBuffer changes outside of your control the index will not be the same.

I don't think you can do what you want without calling indexOf.

What you could do is create your own object that wrapped a StringBuffer (or StringBuilder depending on your needs - or just a List) and then every time a new String was append to your own object, you could control the index that you want to keep in the way Bill said.

However, why is it a problem to call indexOf?
[ November 07, 2008: Message edited by: Rory Marquis ]
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
my problem is to check indexOf latest appended value(may be same) for the StringBuffer.
string will be like this mmm<br />hhh<br />kkk
i need to check all possible indexOf <br /> with only one time previous <br /> should not be repeated

i am using following method.
public static String makeReplyOrForwardMessage(String s)
{
// ensure that each line of the text begins with "> ".
StringBuffer sb = new StringBuffer();
boolean nextLine = false;
for (int i=0;i<s.length();i++)
{
char c = s.charAt(i);
if (c == '\n' || c == '\r')
nextLine = true;
if (nextLine && c != '\r' && c != '\n')
{
sb.append("> ");
nextLine = false;
}
sb.append(c);
}
return sb.toString();
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please use the code button.
Beware with indexOf: if there are two copies of the same text inside your StringBuffer (StringBuilder would probably be better) you need to be very careful about which you find.
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
I didn't get you what you are trying to say could you please give small example for my problem it may solve my problem i don't understand why we need to use [code] tags could you please give small example if you have enough time please apologise me if you don't have enough time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
public void printWithoutCodeButton(String s)
{
System.out.format("%s has not used the code button to print \"%s\"" +
" and we can see how difficult the code is to read.%n",
"XXX", s);
}//end print without code buttonAll is well until you have(or "String" or "in"). You would usually pick up the first instance of those Strings in the builder, but what if you really want the second occurrence?
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
yes campbell
Rory Marquis
Ranch Hand

Joined: Nov 03, 2008
Posts: 48
You could use Regex or the Scanner API. To make sure there were only the number of String matches you wanted.

Of course your index in the StringBuffer would still change if there were characters append before that index.
N Naresh
Ranch Hand

Joined: Nov 04, 2008
Posts: 66
how we can get 2nd and 3rd and 4th an so on for the occurence of a particular string
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Go to the StringBuilder API page and you find the indexOf method is overloaded.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Looping StringBuffer