473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Delete last 4 characters from a given string.

1
x="A4478596895";
delete last four characters.
Mar 29 '12 #1
5 25336
For a C++ string class, use the erase and length function:
Expand|Select|Wrap|Line Numbers
  1. x.erase(x.length()-4);
This erase member takes the starting position (left-most) of the characters to remove, and takes in a second optional parameter which is the number of characters to remove. If the second parameter is not specified, all the characters from the starting position will be removed. (The erase thus shortens the string by the number of characters removed.)

Just for info, if your string is a C string, simply set the character after the last character to 0 (null terminate it):
Expand|Select|Wrap|Line Numbers
  1. x[strlen(x)-4]=0;
Mar 29 '12 #2
Hmm... it works right? I don't know who reset my answer.

Edit: I did test my solution before writing this post, and it works... It is, in my opinion, probably the most efficient one, because it does the operation directly on the string using a built-in C++ function without any loops.
Mar 30 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
The best approach is to iterate the string from right to left. Just use rbegin() an iterate 4 times doing and erase on each iteration. The string will slide to the right each time.
Mar 30 '12 #4
x = x.substring(0,x.length()-4);
Mar 31 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
That is even better.
Mar 31 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Marcin Wolku | last post by:
hi i have a question how can i delete last added row. I have 2 tables . source and destination . I take a 1 row from source table , do some operation on it and save to destination table . after...
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
3
by: Ryan Liu | last post by:
Hi there, I got a NullReferenceException when delete last row in a datagrid. I had hard time to solve since it does not occur in my own code. I put a datagrid in my inherited user control,...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at a time and then check for the given string? ...
3
by: Cc | last post by:
how do i delete last append line?
16
by: sangram | last post by:
how to delete last node of a Linked list if you only know the address of last node. thanks sangram
2
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code...
1
by: vaskarbasak | last post by:
Hi All, 1.How can i convert a String to Unicode characters? 2.Have there any way to get the Unicode range for a given String? Thanks! vaskar
16
by: saki | last post by:
Write a program to print all the permutations of a given string.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.