Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi guys i want to trim a string after a special character..

lets say the string is str="arjunmenon.uk"
i want to get the characters upto the . and ignore the rest.i.e the resultant string must be restr="arjunmenon"
Posted

Try this

C#
string str = "this is a #string";
string ext = str.Substring(0, str.LastIndexOf("#") + 1);



THis should work. tweak it as pr your needs.
 
Share this answer
 
Comments
AshishChaudha 28-Jun-12 4:38am    
my 5!
Rahul Rajat Singh 28-Jun-12 6:55am    
thanks.
Arjun Menon U.K 28-Jun-12 7:39am    
Thanks Rahul ..
Rahul Rajat Singh 28-Jun-12 7:41am    
You are most welcome. always ready to help fellow developers.
King Fisher 21-Apr-14 0:35am    
nice one :)
Hi check this once I think it works for u
C#
string str = "asd.uk";
            if (str.Contains('.'))
            {
                int index = str.IndexOf('.');
                string result = str.Substring(0, index);
                Console.WriteLine("result: " + result);
            }
 
Share this answer
 
Comments
Lindo Mncwabe 22-Oct-13 3:13am    
this code still rocks
thanks
Ade.Ruyani.Z 21-Jun-14 1:46am    
thanks..
 
Share this answer
 
I achieved my solution in this way

C#
string[] FinalArr = { }; // An array
        if (RawAddedItems.Length > 1) //<rawaddeditems is my data
        {
            if (RawAddedItems.Contains("OrderPageRadios"))//To confirm the data which needs to be parsed
            {
                int index = RawAddedItems.IndexOf("OrderPageRadios"),
                    secondindex = RawAddedItems.IndexOf("[", index) + 1,
                    LastIndex = RawAddedItems.IndexOf("]", index);

                string NewDBString = RawAddedItems.Substring(secondindex, LastIndex - secondindex);

                FinalArr = NewDBString.Split("|");
                return FinalArr;
            }

            return FinalArr;
        }
        else
            return FinalArr;
    }


Any questions regarding the code can be answered :)
 
Share this answer
 
v2
C#
using System;

class Program
{
    static void Main()
    {
    const string s = "Dot Net Perls is about Dot Net.";
    Console.WriteLine(s);

    // We must assign the result to a variable.
    // ... Every instance is replaced.
    string v = s.Replace("Net", "Basket");
    Console.WriteLine(v);
    }
}
 
Share this answer
 
Comments
CHill60 7-Jun-15 19:46pm    
The question is 3 years old.
The question is answered.
The question is NOT answered by the code you have posted.
The question IS answered by Solution 1 and/or Solution 2
Member 10530718 7-Apr-16 5:50am    
string str = "Company is Great";
i want to do like "great is Company" tell me how to do it
Member 12678328 20-Aug-16 20:02pm    
That other guy is mean
Member 12678328 20-Aug-16 20:02pm    
you helped buddy dont u worry
Member 13575125 13-Jul-18 15:32pm    
i have a string like this
string a=reg/13/07/2018/0001
i want just last 4 digit
plese tell me how i am solve this issue

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