Solved

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

Posted on 2010-08-24
10
284 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

16 Experts available now in Live!

Get 1:1 Help Now