Solved

Truncate and remove empty sapce and comma in the string

Posted on 2013-05-26
17
279 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 74

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 74

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
 

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 74

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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 74

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 74

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 74

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 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

15 Experts available now in Live!

Get 1:1 Help Now