Solved

Truncate and remove empty sapce and comma in the string

Posted on 2013-05-26
17
284 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

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!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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