Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code below using to copy items in listBox1, and it works.
If possible I would like to edit this code, so that to copy only part of the item, not the whole item.

example:
if the content item:

1. Name Surname
be copied only: Name Surname
but not to be copied: 1. Name Surname (1. - not to be copied)

Note: 1. is always in a series, and 1. does not change.

C#
private void Copy()
{
    StringBuilder sb = new StringBuilder();

    foreach (string sLine in listBox1.SelectedItems)
    {
        sb.Append(sLine + Environment.NewLine);
    }

    if (!string.IsNullOrEmpty(sb.ToString()))
    {
        Clipboard.SetText(sb.ToString(), TextDataFormat.Text);
    }
}


What I have tried:

C#
Clipboard.SetText( string.Concat( listBox1.SelectedItems.Cast<string>().Select( item =>
 Regex.Match( item, @"^\d+\.\s+(.+)" ).Groups[1].Value + Environment.NewLine ) ) );


But this is not the solution ...
Posted
Updated 7-Oct-16 2:28am
v2
Comments
Ralf Meier 7-Oct-16 6:07am    
Does each Item of your Listbox begin with '1.' ?
Or does each Item begin with a (incrementing) number followed by a Dot ?
Bilgehan Berberoğlu 7-Oct-16 6:58am    
Which platform you use when implement your app? (Winform, wpf, etc...)

The simplest way is to check if it exists and if so, discard it:
C#
string addme = sLine;
Match match = Regex.Match(sLine, @"(?<=1\.\s+).*");
if (match.Success)
    {
    addme = match.Value;
    }
sb.AppendLine(addme);
 
Share this answer
 
Comments
Member 10410972 7-Oct-16 10:51am    
Thanks you.
OriginalGriff 7-Oct-16 11:08am    
You're welcome!
Maybe I'm wrong but this is a very generic question: how can I cut off a part of a string?

In your case you get a string "1. Name Surname" and need to cut off the leading "1." like e.g. this:

C#
foreach (string sLine in listBox1.SelectedItems)
{
   //sb.Append(sLine + Environment.NewLine);
   sb.Append(sLine.Replace("1.", "") + Environment.NewLine);
}


Better you use the regular expression - this is very good documented as well, e.g. Regex.Replace-Methode (String, String)[^]
 
Share this answer
 
v4
Comments
Member 10410972 7-Oct-16 10:51am    
Thanks for your solution, I accepted.
And whether it is possible and something like this:

foreach (string sLine in listBox1.SelectedItems)
{
//sb.Append(sLine + Environment.NewLine);
sb.Append(sLine.Replace("1.", "" || "2.", "") + Environment.NewLine);
}
C#
Clipboard.SetText(string.Join(Environment.NewLine,
    listBox1.SelectedItems.Cast<string>()
        .Select(item=>Regex.Match(item, @"^\d+\.\s+(.+)"))
        .Where(match=>match.Success)
        .Select(match=>match.Groups[1].Value)
    ));
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 7-Oct-16 10:05am    
:-) lol
Member 10410972 7-Oct-16 10:51am    
Thanks you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900