aspose file tools*
The moose likes Beginning Java and the fly likes how to edit the last 3 letters of a string retrieved from database? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to edit the last 3 letters of a string retrieved from database? " Watch "how to edit the last 3 letters of a string retrieved from database? " New topic
Author

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

chrstian ferma
Ranch Hand

Joined: Mar 26, 2014
Posts: 67

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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

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.
Rico Felix
Ranch Hand

Joined: Mar 08, 2014
Posts: 289
    
    4

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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11420
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
Don't get me started about those stupid light bulbs.
 
subject: how to edit the last 3 letters of a string retrieved from database?