Solved

Truncate and remove empty sapce and comma in the string

Posted on 2013-05-26
17
281 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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