Click here to Skip to main content
11,578,006 members (65,566 online)
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 22: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
Ganesh Vellanki at 10-Dec-14 7:33am
   
Wow really nice 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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);
    }
}
  Permalink  
Comments
CHill60 at 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

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

  Print Answers RSS
0 Richard MacCutchan 165
1 Peter Leow 150
2 OriginalGriff 120
3 Afzaal Ahmad Zeeshan 100
4 Frankie-C 85
0 OriginalGriff 880
1 Sergey Alexandrovich Kryukov 685
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 393


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 6 Jun 2015
Copyright © CodeProject, 1999-2015
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