• 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

how to edit the last 3 letters of a string retrieved from database?

 
Ranch Hand
Posts: 72
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just want to have an idea how to do this editing the last 3 letters of a string that i retrieve from database

i have a string "111-222-333-000" here's the sample what i want to happen was to edit the last 3 letters of the string

i insert into database "111-222-333-000"
then i retrieve it for editing but what i want to happen is when i retrieve it what i can only edit was the last 3 strings only thank you
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying you want to turn that into the String "000"?

There are two ways of doing that:
1) Use the substring() method. Hint: use the signature that takes one parameter passing the index of 3 characters before the end.
2) Use the replaceFirst() method with a regular expression to replace all but the last three characters. Hint: "\d{3}$" matches the last three characters.

The second option might be too advanced depending on your level of Java knowledge.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java's String class has a substring() method (two actually) for extracting a part of the string. It doesn't matter if the string came from a database, a file, or was just created in Java code. Note that Java Strings are immutable, meaning they can't be changed. That doesn't matter for you though. You can create a new String based on the contents of the original String's contents and your edits, and then update your database with the new String.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a method that you can use to acquire ideas to achieve your goal:



How it works:

Retrieve the string from your database and pass it in as an argument to the function... the function will run a regex (Regular expression) on the string searching for the last three digits and replacing it with what the user enters...This creates and returns the new string which you can insert back to the database...

The key to this code is the regex:

\\d search for digits
$ ends with
{3} search for 3

Put together it says; search for 3 digits at the end of the string... used with the aid of replaceFirst(String regex, String replacement) to perform the edit
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chrstian ferma wrote:Just want to have an idea how to do this editing the last 3 letters of a string that i retrieve from database


Most of the answers so far have concentrated on dealing with this code as a String. My question is: Is it really just a String?

The formatting would suggest that it's actually a representation of something very specific (like, for example, a phone number, or a SKU, or a barcode) and, in that case, I'd suggest creating a Java class that encapsulates it, because THEN you can add logic to update any part of it you like.

Just because the database happens to store it as a String doesn't mean that you have to. Java is an Object-Oriented language, so things usually work best if you use strongly-typed objects to store your information; and I'd say that "111-222-333-000" is simply a String representation of something that isn't actually a string at all.

My 2¢

Winston

PS: You may be interested in reading the StringsAreBad page, which expands on this exact point.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your method should also not care HOW YOU GOT the string. regardles of whether you got it from a database, read it from a file, or got is as user input...editing the last three letters is independent of that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic