• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looping StringBuffer

 
N Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the stringbuffer changes then how we can achieve
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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))
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes campbell
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how we can get 2nd and 3rd and 4th an so on for the occurence of a particular string
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to the StringBuilder API page and you find the indexOf method is overloaded.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic