Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Truncate and remove empty sapce and comma in the string

Posted on 2013-05-26
17
Medium Priority
?
287 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:Meir Rivkin
Meir Rivkin earned 1000 total points
ID: 39198469
string name = name.Replace(' ','').Replace(',','').Substring(0,7);
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 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
Independent Software Vendors: 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:Meir Rivkin
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:Meir Rivkin
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:Meir Rivkin
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

885 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