• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looping StringBuffer

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the stringbuffer changes then how we can achieve
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes campbell
 
Rory Marquis
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go to the StringBuilder API page and you find the indexOf method is overloaded.
 
reply
    Bookmark Topic Watch Topic
  • New Topic