Skip to main content
Announcements
July 15, NEW Customer Portal: Initial launch will improve how you submit Support Cases. IMPORTANT DETAILS
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

How to get last 7 characters of a string?

Hi Anyone,
I need help regarding this:
I'm using tMap and I'm trying to get the last 7 characters of the strings that I'm loading to a new target.
Example: "0344334382" will be "4334382" 

Labels (2)
12 Replies
TRF
Creator III
Creator III

Hi,
This should works:

StringHandling.RIGHT(row17.phone, 7) 


Regards,
TRF
Anonymous
Not applicable
Author

Hi TRF,
It didn't work for me, this is my expression:
row2.Status.equals("Telephone number") ?
StringHandling.RIGHT(row2.Telephone,7) : row2.Telephone


Thanks!
Anonymous
Not applicable
Author

What error did you get?
Also, the code you used seems a little odd.

row2.Status.equals("Telephone number") ? [size=1][font=Verdana, Helvetica, Arial, sans-serif][/font][/size]
StringHandling.RIGHT(row2.Telephone,7) : row2.Telephone

Is "Telephone number" actually used? This will only ever result in " number" or whatever is in row2.Telephone untouched.

You are not handling potential null situations either. 
Anonymous
Not applicable
Author

Hi rhall_2.0,
I get no error but when I try to query in the target db, it is not updated but if I don't use the if else statement, it works fine.
To answer your question, it is because in my source table I have this:
Telephone                   Status(This should be actually named as Type not Status)
0923477324            Cellphone number
32594385                Telephone number
That is the reason why I have the  "Telephone number"
TRF
Creator III
Creator III

Hi,
What is your result?
I just tried with following values:


The result is:


I've change my schema according to yours and tried this:
row17.status.equalsIgnoreCase("phone") ? StringHandling.RIGHT(row17.phone, 7) : row17.phone 


Works fine for me.
Anonymous
Not applicable
Author

OK, my mistake. I misread the code.
Try the following code. There are lots of things that can cause a String that seems to match to not match. You need to remove those possibilities.
row2.Status.trim().compareToIgnoreCase("Telephone number")==0 ? 
StringHandling.RIGHT(row2.Telephone,7) : row2.Telephone

 What the above does is trim the Status to ensure there are no extra spaces before or after "Telephone number", it then compares it while ignoring the case of the String. 

Can you be sure that row2.Telephone will always have a value? If so the above it fine. If not, you will have to also check for nulls.
Anonymous
Not applicable
Author

Hi TRF and rhall_2.0,
I think I already got it in the first place, I just made a mistake when comparing my string, the value is "Telephone numbers" and i'm trying to compare it to "Telephone number". I should really be careful, my bad.
Just to answer rhall_2.0's question, I'm sure that there are no extra spaces because I'm the one who created the Type(previously status) column via tMap. Is it a best practice to always put a trim() to ensure no extra spaces? if it is, starting now I'll always use that. I only use that whenever I'm sure that there are heading and trailing spaces.
For the 2nd question, the answer is NO. Should I put error handler for nulls? In TRF's code (correct me TRF if I'm wrong), NULLS are returned as Blanks or Nulls? Either way, it is okay to return blank and null values because I will ask the data owner to fill up missing data.
Thank you TRF and rhall_2.0
Anonymous
Not applicable
Author

If you have total control over your data, then I would say best practice is to rely on what you KNOW will be the case and minimise CPU cycles. My suggestion was more of a "catch all" suggestion since I didn't know you had control over the data.

With regard to nulls they can be dangerous if not handled. When you instantiate an object to be null (nothing), you cannot make use of the object's functions/methods. So "String.compareToIgnoreCase()" would result in an error if the String is null. As such it is always a good idea to check for nulls IF you do not KNOW that they will not exist in your data.
TRF
Creator III
Creator III

Hi Johnpaul,

StringHandling.RIGHT(row17.phone, 7) returns null if row17.phone == null but it doesn't fail.
Changing to:

row17.status.equalsIgnoreCase("phone") ? ">" + StringHandling.RIGHT(row17.phone, 7) + "<" : row17.phone

give the following result:
[statistics] connecting to socket on port 3424
[statistics] connected
>null<
>123<
>1234567<
>2345678<
[statistics] disconnected


Kind regards,
TRF