Solved

Truncate and remove empty sapce and comma in the string

Posted on 2013-05-26
17
282 Views
Last Modified: 2013-05-27
Hi All,

I have this string like below as my input

string Name ="CATON, DEAN SCOTT"

Am trying to find ,is there any comma in that string and empty string,if there is then eliminate the comma and empty string and truncate the string to Lenght (11).

So my final out put am expecting :CATONDEANSC.

Some one please advise me the best and fastest way to do this.

Thanks in Advance.
0
Comment
Question by:Sha1395
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
17 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39198468
You could do something like this:

e.g.

static string ShortenTo(string original, int shortenToLength)
{
    StringBuilder result = new StringBuilder();
    int i = 0;

    if (original == null)
    {
        return null;
    }

    while (result.Length < shortenToLength && i < original.Length)
    {
        if (char.IsLetter(original[i]))
        {
            result.Append(original[i]);
        }

        i++;
    }

    return result.ToString();
}

Open in new window

0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 250 total points
ID: 39198469
string name = name.Replace(' ','').Replace(',','').Substring(0,7);
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39198470
You could also use LINQ:

string result = new string(Name.Where(c => char.IsLetter(c)).Take(11).ToArray());

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Sha1395
ID: 39198471
Damn,

I was trying something like this

if (resourceName.Trim().Contains(" "))
                {
                    resourceName = resourceName.Replace(" ", "");
                }
                if(resourceName.Trim().Contains(","))
                {
                    resourceName = resourceName.Replace(",", "");
                }
                if (resourceName.Length > 11)
                {resourceName= resourceName.Substring(0, 10); }

Open in new window

0
 

Author Comment

by:Sha1395
ID: 39198472
Thanks a lot Kaufmed and sedgwick. why we all people call you guys guru.

Thanks a lot
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39198473
@Kaufmed
That would truncate numbers numbers.
If linq then maybe
New String(name.Where(c=> !(new char[]{' ',','}).Contains(c)))
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39198475
@sedgwick

Agreed, but the example didn't show numbers, so I didn't include them. It could certainly be adjusted to accommodate various accepted characters.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39198478
That should have been split points.
I gave first correct answer while kaufmed was not.
0
 

Author Comment

by:Sha1395
ID: 39198479
Sorry Sedgwick,

I was trying to split the point from the first hand itself but some how all the points gone to Kaufmed.Now i have no idea how to edit. Please advise me
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39198482
@sedgwick
I gave first correct answer
Technically speaking, you did not since you truncated the string to 7 characters and the requirement was for 11. It's up to the asker as to how points are distributed...you know that  : )

@Sha1395

You can "Request Attention" (just below your original question) if you would like to edit the point distribution.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39198485
@sedgwick

P.S.

And if we were going to really split hairs, the requirement was for the "fastest" implementation. Your approach creates 3 additional strings whereas my first post does not. The difference wouldn't be noticeable in ordinary usage, though, so I didn't press the issue.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39198486
U right. Its too early in the morning i guess
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39198487
Your approach creates 3 additional strings whereas my first post does not.
I should rephrase that to, "Your approach creates 2 intermediate strings whereas my first post does not."
0
 

Author Comment

by:Sha1395
ID: 39198492
Hey guys, I sent the request to the moderator but could you guys please suggest me what am doing in my code is right  ?

FYI

Hi,

I would like to split my point 250 to Kaufmed and 250 to Sedgwick. here is the code am using in my code
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39198506
The only thing I see is that you are only selecting 10 characters in your Substring call when you should be selecting 11, per your requirement.
0
 

Author Closing Comment

by:Sha1395
ID: 39198534
Thanks Kaufmed.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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