Solved

How do you TRIM n characters from a string from the right.

Posted on 2010-08-24
10
281 Views
Last Modified: 2013-12-17
I should trim last name of a employee to 18 characters even though if I get a 30 character last name from the database.
What is the best way to do that ? any help would be greatly appreciated.

Thanks
0
Comment
Question by:prashant-Vanam
  • 4
  • 4
  • 2
10 Comments
 
LVL 9

Expert Comment

by:wellhole
ID: 33512257
lastname = lastname.substring(0, 18) // cut name starting from 0 for 18 characters
0
 
LVL 14

Expert Comment

by:systan
ID: 33512312
Should this be;
text = text.substring(19, 30);
0
 
LVL 14

Expert Comment

by:systan
ID: 33512356
Oh, I have it wrong, wellhole got it.
0
 
LVL 14

Expert Comment

by:systan
ID: 33512389
He got it but with wrong comment, that's why I reverse it, but I got it wrong it code instead.

>>// cut name starting from 0 for 18 characters
// cut name starting from 19 to the last characters, instead.
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512399
str = str.Substring(0, 18);
str=str.Remove(18);

which one is better to use Substring or Remove
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 14

Expert Comment

by:systan
ID: 33512528
str=str.Remove(18);

substring uses remove
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512827
Substring and remove throws exception if the length of the string is less than 18 characters.
0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512867
This is my code ...I'm wring to a fixed length text file and I need to make sure that the length of the string doesnt break the pattern of fixed length text file. That means the length of the lastname should never be more than 18 characters. If it is more than 18 characters in the DB then I should truncate it to 18 characters.
using (var sw = new StreamWriter(txtFile))
            {
                foreach (Employee employee in lstEmployees)
                    sw.WriteLine(String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}",
                                               employee.Ssn.Substring(0,9).PadRight(9),
                                               employee.EmployeeLastName.Substring(0,18).PadRight(18),
                                               employee.EmployeeFirstName.Remove(12).PadRight(11),
                                               employee.EmployeeMiddleName.Remove(1).PadRight(2),
                                               employee.EmployeeAddress1.Remove(46).PadRight(46),
                                               employee.EmployeeCity.Remove(16).PadRight(16),
                                               employee.EmployeeState.Remove(2).PadRight(2),
                                               employee.EmployeeZipCode.Remove(5).PadRight(5),
                                               employee.EmployeeZipCode4.Remove(21).PadRight(21),
                                               (string.IsNullOrEmpty(employee.HireDate) ? string.Empty : String.Format("{0:yyyyMMdd}", Convert.ToDateTime(employee.HireDate))).Remove(8).PadRight(8),
                                               (string.IsNullOrEmpty(employee.BirthDate) ? string.Empty : String.Format("{0:yyyyMMdd}", Convert.ToDateTime(employee.BirthDate))).Remove(8).PadRight(9),
                                                employee.CompanyName.Remove(30).PadRight(30),
                                                employee.CompanyAddress1.Remove(46).PadRight(46),
                                                employee.CompanyCity.Remove(16).PadRight(16),
                                                employee.CompanyState.Remove(2).PadRight(2),
                                                employee.CompanyZipCode.Remove(5).PadRight(5),
                                                employee.CompanyZipCode4.Remove(21).PadRight(21),
                                                employee.FederalId.Remove(52).PadRight(52),
                                                State.PlaceHolder.Remove(1).PadRight(1)));

            }

Open in new window

0
 
LVL 1

Author Comment

by:prashant-Vanam
ID: 33512998
I will do this in the worst case........please let me know if anyone has any better solution.

Thanks
using (var sw = new StreamWriter(txtFile))
            {
                foreach (Employee employee in lstEmployees)
                    sw.WriteLine(String.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}",
                        (employee.Ssn.Length > 9 ? employee.Ssn.Substring(0, 9).PadRight(9) : employee.Ssn.PadRight(9)),
                        (employee.EmployeeLastName.Length > 18 ? employee.EmployeeLastName.Substring(0, 18).PadRight(18) : employee.EmployeeLastName.PadRight(18)),
                            }
}

Open in new window

0
 
LVL 9

Accepted Solution

by:
wellhole earned 500 total points
ID: 33513442
That's actually among the best ways to do it. You can remove the PadRight from the Substring.PadRight because you're already guaranteed 18 length.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now