Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 27-Jun-12 23:19pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

THis should work. tweak it as pr your needs.
  Permalink  
Comments
AshishChaudha at 28-Jun-12 4:38am
   
my 5!
Rahul Rajat Singh at 28-Jun-12 6:55am
   
thanks.
Arjun Menon U.K at 28-Jun-12 7:39am
   
Thanks Rahul ..
Rahul Rajat Singh at 28-Jun-12 7:41am
   
You are most welcome. always ready to help fellow developers.
King_Fisher at 21-Apr-14 0:35am
   
nice one :)
Ade.Ruyani.Z at 21-Jun-14 1:45am
   
thanks rahul, helpfull code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi check this once I think it works for u
string str = "asd.uk";
            if (str.Contains('.'))
            {
                int index = str.IndexOf('.');
                string result = str.Substring(0, index);
                Console.WriteLine("result: " + result);
            }
  Permalink  
Comments
Lindo Mncwabe at 22-Oct-13 3:13am
   
this code still rocks
thanks
Ade.Ruyani.Z at 21-Jun-14 1:46am
   
thanks..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I achieved my solution in this way
 
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 Smile | :)
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 274
1 Shweta N Mishra 216
2 PIEBALDconsult 210
3 Sergey Alexandrovich Kryukov 185
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 23 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100