Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to remove between Chars which
ex:
C#
string s=website(14)\\.net(4);

i want only
like s=website\\.net can u guide or send any snippets'
Posted
Updated 14-May-12 1:32am
v3
Comments
Tejas Vaishnav 14-May-12 7:31am    
It always same like (14) and (4) or any thing else.

The Solution 1 given by Mohammad A Rahman using LINQ is good. Another alternative, is to use Regex as shown below.

C#
string text = "s=website(14)\\.net(4)";
string requiredText = Regex.Replace(text,@"\(\d*\)","");
Console.WriteLine (requiredText);

//Output
//s=website\.net
 
Share this answer
 
v3
Comments
Mohammad A Rahman 14-May-12 7:48am    
Thank you. It is good point. Please take my little appreciation 101:)
VJ Reddy 14-May-12 7:50am    
Thank you, Rahman.
It might help you,
C#
using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = @"website(14)\\.net(4)";
            var result = new String(s.Where(x => !Char.IsNumber(x) && x!='(' && x!=')'  ).Select(x => x).ToArray());
            Console.WriteLine(result);
        }
    }
}


This will produce following output,
website\\.net
Press any key to continue . . .


For more information about the Where and Select please see How does it work in C#? - Part 3 (C# Linq in detail)[^]

Hope it helps :)
 
Share this answer
 
Comments
VJ Reddy 14-May-12 7:45am    
Good answer. 5!
kingsa 14-May-12 7:57am    
hello,
thank u for great responsebut i need wesbsite12(15)\\.net(5)
i want like website12\\.net
You can use Regex:

C#
string s = @"website(14)\\.net(4)";
string result = Regex.Replace(@"\(\d*\)", string.Empty);
Console.WriteLine(result);
 
Share this answer
 
hi
this will remove all string within '()'

C#
string RemoveStr(string input)
       {
           string s = "";
           try
           {
               string[] strs = { };
               if (input.Contains("(") || input.Contains(@")"))
               {
                   strs = input.Split('(');
                   foreach (var item in strs)
                   {
                       if (item.Contains(@")"))
                           s += item.Substring(item.IndexOf(@")") + 1);
                       else
                           s += item;
                   }
               }
           }
           catch (Exception)
           {

               throw;
           }
           return s;
       }
 
Share this answer
 
Hi,

I hope this will work,

C#
string Input = website(14)\\.net(4);        
string Output = string.Empty;         
foreach (var c in s)         
{             
  int ascii = (int)c;             
  if (!(ascii >= 48 && ascii <= 57) || ascii == 40 || ascii == 41)                 
  result += c;         
}         
Console.Write(result);
 
Share this answer
 
v2

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