• Post Reply Bookmark Topic Watch Topic
  • New Topic

Append another string at 150th location of current String.  RSS feed

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I have a requirement.
I have a real long String(over 3000 characters)
I need to check if the string length is > 150, if yes, check for the lastIndexOf("','"). If present, then append a at that location.
This should happen till the end of String is reached or no more occurence of "','" is left.

Please help.
Code attached.


Thanks all in advance.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using StringBuffer instead of StringBuilder?
Why aren't you using a method to insert something?
You are not clear about your requirements; you say you need the last comma, and then you need to go to the end of the String. Do you mean back to the start of the String?
I think you will find it easier to work backwards from the end of the String to its beginning.
 
Viidhya Kishore
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
My code is the starting point.

My requirement is
if the string is longer than 150 characters, check for the last occurencec of ',' from character 0 to 150 and then append 'break' at that location.
Then select the next 150 chars , search for ',' then do the same thing.
Eg of the String is

PI Last Name equal to one of ('AARONSON','ABDEL MEGUID','ABDEL-LATIF','ABDOOL KARIM','ABELL','ABRAMS','ACKERMAN','ADAIR','ADAMS','ADAMS-CAMPBELL', 'ADASHI','ADEBAMOWO','ADHIKARI','ADIMORA','ADRIAN', 'ADZERIKHO','AGADJANYAN','AGARWAL','AGOT', 'AGUIRRE-CRUZ','AHMAD','AHMED','AIKEN', 'AINAMO', 'AISENBERG','AJAIYEOBA','AKA','AKHTAR','AKINGBEMI','AKINYINKA','AKKERMAN','AKSOY','AKYUREK', 'ALBEROLA-ILA','ALBERT','ALCANTARA' ,'ALCOCK','ALEMAN', 'ALEXANDER','ALEXANDRE','ALEXANDROV','ALEXANIAN','ALLAND','ALLEN','ALLISON','ALPER', 'ALTMAN','ALVAREZ','AMARYAN','AMBESI-IMPIOMBATO','AMEGBETO','AMOWITZ', 'ANAGNOSTARAS','ANAND','ANDERSEN','ANDERSON', 'ANDRADE','ANDREEFF','ANDROPHY','ANGER','ANHOLT','ANTHONY','ANTLE','ANTONELLI','ANTONY', 'ANZULOVICH', 'APODACA','APOSHIAN','APPEL','APPLEBY','APRIL','ARAUJO','ARBIB','ARBOLEDA', 'ARCHAKOV','ARCHER', 'ARECHAVALETA-VELASCO','ARENS','ARGON','ARGYROKASTRITIS', 'ARIAS','ARIZAGA','ARMSTRONG','ARNON', 'ARSHAVSKY','ARVIN','ASATRYAN','ASCOLI','ASKENASE','ASSI','ATALAY','ATANASOVA','ATKINSON','ATTYGALLE','ATWEH','AU','AVETISYAN','AWE','AYOUB','AZAD','BACSO','BAGASRA','BAKER','BALAS', 'BALCAZAR','BALK','BALKAY','BALLOU','BALRAJ','BALSTER','BANERJEE','BANKOLE','BANTA','BARAL','BARANOWSKA','BARBAS', 'BARBER','BARILLAS-MURY','BARKHOLT','BARNES','BARNETT','BARRETT','BARRIA','BARROW','BARROWS','BARTKE','BARTLETT','BASSINGTHWAIGHTE','BASSIOUNY','BASU','BATES','BATTAGLIA','BATTERMAN','BAUER','BAUERLE','BAUM','BAUME', 'BAUMLER','BAVISTER','BAWA','BAYNE','BEASLEY','BEATTY','BEATY','BEBENEK','BECK','BECKER','BECKMAN','BECKMAN-SUURKULA' ,'BEDFORD','BEDOLLA','BEEBE','BEEMON','BEHETS','BEHRMAN','BEIER','BEKKER','BELL','BELLIDO','BELMAIN', 'BENATAR','BENBENISHTY','BENBROOK','BENDER','BENEDETTI','BENNETT','BENNISH','BENZ','BERG','BERGER','BERGEY','BERGGREN','BERK','BERKOWITZ','BERLIN','BERLINER','BERMAN','BERTINO','BERTOZZI','BERTRAND','BERWICK','BETHONY','BEYERS','BEYRER' ,'BEZPROZVANNY','BHAGWAT','BHANDARI','BHARGAVA','BHARUCHA','BHUJWALLA','BIANCO','BIDLACK','BIELERT','BIER','BIESSMANN','BIGELOW' ,'BILLER','BILLINGS','BINDER','BINDMAN','BINUTU','BIRBECK','BIRGE','BIRNBAUM','BIRO','BIRT','BISHAI','BISHOP','BISSELL','BJORKEGREN','BJORNSTAD','BLACK','BLANCHARD','BLASS','BLATTNER','BLIGNAUT','BLOCH','BLOCK','BLOOM','BLOOM,','BLUM','BLUMBERG' ,'BLUMENTHAL','BLYUKHER','BODDULURI','BOFFETTA','BOGOLIUBOVA', 'BOLLINGER','BOLLS','BOMSZTYK','BONANNO','BONNER','BOOM','BOOTHROYD','BOPPANA','BORAWSKI','BORG','BORIS-LAWRIE','BORISY','BORLONGAN','BORNSTEIN','BORODOVSKY','BORST','BOS','BOTO','BOWDEN','BOWEN','BOYCE-JACINO','BRADEN','BRADY' ,'BRAITHWAITE','BRANN','BRASH','BRAUNSTEIN', 'BREMAN','BRENNAN','BRENNER','BRETSCHER','BREW','BREYSSE','BRIGGS','BRITES','BRITT','BRITTENHAM','BRODIE','BRODY','BROOK','BROOTEN','BROSCO','BROSNAN','BROWN','BROWNE','BRUCKNER','BRUNENGRABER','BRYL','BRYSON','BU','BUCHAN','BUDD','BUDNIK', 'BUEKENS','BUKRINSKY','BULLMORE','BULUN','BURBANO','BURGENER','BURGESS','BURKS','BURMEISTER','BURNETT','BURNHAM','BURNS','BURRIDGE','BURTON','BUSCIGLIO','BUSHEK','BUSIJA','BUZSAKI','BZYMEK','CABA')

I need to insert a break at the last occurence of ',' within every 150 chars.

Hope I am clearer this time.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should be able to work it out easily from the requirement. Count length / 150 * 150, find last index of comma, insert, reduce your index by 150, repeat.
You will have to alter that slightly if you absolutely insist there be no Strings longer than 150 displayed.

There is a little twist which I have omitted.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have taken the code tags off one of your posts because text doesn't wrap inside code tags.
 
Viidhya Kishore
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me a code snippet?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Viidhya Kishore wrote:Can you give me a code snippet?

That's not what we do here. Our goal is to help YOU code it, not give you the code.

It works better her if you try it, then post the code you wrote and tell us what it does vs. what you think it should do.
 
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that this is not an assignment for school/college/university where you are not allowed to use third party components then this is a simple job
for class Rewriter . You will need a regular expression that greedily looks for up
to 150 characters with a last character being a ','. The check for the last character can be done using negative look behind. The replacement will just be


 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sabre wrote: . . . class Rewriter . . . .
That looks useful; I had never heard of it.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
James Sabre wrote: . . . class Rewriter . . . .
That looks useful; I had never heard of it.


Very useful; I use it a lot. Since I still can't be sure that this is not homework I shall wait a couple of days and then post a solution to the OP's problem using Rewriter.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave him enuigh hints earlier. . . .
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have moved the last few posts to a new topic, so that the discussion about the changes made by a moderator can be discussed separately.

Quick summary of the spinoff topic. A ranchhand posted an example, or solution. A moderator deleted it. Another moderator commented on it. And the beginning of a discussion about this ...

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sabre wrote:I shall wait a couple of days and then post a solution to the OP's problem using Rewriter.

Please don't provide a solution. If you do, it will most likely be deleted. We encourage people to write their own code, and to help others do the same. The staff feels this is the better approach than providing a complete answer, regardless of the amount of time that has passed since the original request.

thanks
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
James Sabre wrote:I shall wait a couple of days and then post a solution to the OP's problem using Rewriter.

Please don't provide a solution. If you do, it will most likely be deleted. We encourage people to write their own code, and to help others do the same. The staff feels this is the better approach than providing a complete answer, regardless of the amount of time that has passed since the original request.

thanks


Why would the solution need to be deleted? In the unlikely event that this is a homework assignment then it is probable that the use of 3rd party libraries will not be acceptable so the OP will have to do the work himself anyway. I want to encourage people to make use of existing technology and I feel that Rewriter needs more exposure. By waiting a couple of days I will have given the OP chance to find a solution with or without using Rewriter so he will have put in some effort. The chances are that the OP will not have used Rewriter (I have shown him the water but it is up to him to drink) but I believe that viewers of this thread will be best served by seeing how this problem can be solved using Rewriter. Hopefully then in the future they will be able to apply Rewriter to other problems.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please continue discussion about deleting solutions on the "spin-off" thread linked to in Henry Wong's post. I have replied there.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As promised, my Rewriter solution to this problem :-



Of course the magic number 42 (the answer to the ultimate question) needs to be replaced by 150.

To illustrate the solution I have used an anonymous inner class but in most cases one will create a named class and create just one instance of it. Rewriter
is not thread safe so take appropriate action in multi-threaded use.

I'm not saying that this is the 'best' solution since I don't know what metric will be needed for the 'best' solution. I can say that this will not be the fastest
solution; solutions involving regular expression rarely are.

In practice I would probably use

since it will deal better with overly long fields or fields that did not conform to the assumed format. Any looping solution would also need to deal with these
edge conditions.

If this problem is homework then can you see this being a solution acceptable to the examiner?
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's surprising how simple it looks there. I suspect that i instances of . for "any character" will be quite rapid to scan over.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!