• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Amending a string value with Performance and Efficency in mind

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

A few thoughts about the following would be great,

I have a String that can vary in length from null to 500 characters in length,
When the String contains the name of a book, i need the name of the book to be replaced with %BOOKSTART%book:<nameOfBook>%BOOKEND%,
there can be multiple books in the String (i.e. "There is a book:where and another one here book:here" - will be replaced with "There is a %BOOKSTART%book:where%BOOKEND% and another one here %BOOKSTART%book:here%BOOKEND%"

Whats the best way to do this with performance and efficency in mind?

Thanks in Advance,

Bryan
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Class String has lots of useful methods to search for a substring inside a string and replace text. Those methods are reasonably efficient.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't do this in java at all...this is the kind of project perl or awk excel at. Of course, that assumes you are reading this from a file.
 
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Those methods are reasonably efficient.



And if your strings are only 500 characters long, it very likely isn't worth anybody's time to consider alternative methods. At the present it appears you may be guilty of Premature Optimization.
 
Bryan Murphss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the comments,
I Appreciate the advice,

The String value is coming from an external third party web service so it isn't read from a file or can be pre-factored,

i have the following method:



do you think this is efficent enough or should there be a more better performance way?

Thanks,

Bryan
 
Paul Clapham
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's efficient enough, yes. Although I haven't looked at the code at all. My point is, what I think about the efficiency of your code is irrelevant. If it isn't efficient enough for you, then you should look at doing something about that. Nobody else's opinion matters.
 
Bryan Murphss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your right.

Thanks for the advice
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic