some basic string manipulation stuff

This is a discussion on some basic string manipulation stuff within the C# Programming forums, part of the General Programming Boards category; I am writing some code in C#, in which part of what I am doing requires that I go through ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    some basic string manipulation stuff

    I am writing some code in C#, in which part of what I am doing requires that I go through a string and find the first character that is not a numeral.

    In C++ I could do this quite simply with the string class's find_first_not_of function. In C#, I don't see any equivalent. There is IndexOf, IndexOfAny, LastIndexOf, and LastIndexOfAny, but those obviously aren't what I am looking for.

    So right now I am resorting to this:
    Code:
    string numericValue = string.Empty;
    for (int i = 0; i < userInput.Length && !Char.IsLetter(userInput[i]); i++)
    {
    	numericValue += userInput[i].ToString();
    }
    You might ask: "You are obviously extracting a numeric value (based on your variable naming mechanism), why not just use the LastIndexOfAny method, and pass it a character array of all numeric characters?" My answer: "The user could input numeric characters somewhere later in the string. Hence it wouldn't work."
    My Website

    "Circular logic is good because it is."

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If I understand correctly, the problem you seek to solve is, if you have the string "123abc456", to get the string "123"?
    If so, you could try the regular expression split:
    Code:
    var Parts = System.Text.RegularExpressions.Regex.Split("123abc456", "[^0-9]+");
    This would return an array of strings containing the strings "123" and "456". Get the first element (if Parts.Length > 0, otherwise do something else).

    If you simply want to remove everything except numeric characters, use replace:
    Code:
    var Result = System.Text.RegularExpressions.Regex.Replace("123abc456", "[^0-9]+", "");
    This would return "123456".
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    And as a sidenote, if you want to use this like a normal method:
    Code:
    public static class StringHelper
    {
    	public static string GetFirstNumber(this string String)
    	{
    		var Parts = System.Text.RegularExpressions.Regex.Split(String, "[^0-9]+");
    		return (Parts.Length > 0) ? Parts[0] : "";
    	}
    }
    And call it as such:
    Code:
    var MyText = "123abc456";
    var MyFirstNumber = MyText.GetFirstNumber();
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Quote Originally Posted by DavidP View Post
    I am writing some code in C#, in which part of what I am doing requires that I go through a string and find the first character that is not a numeral.
    To answer this bit directly:

    You can (and probably should) use the extension methods for enumerable types defined under System.Linq.

    The String class implements the IEnumerable interface as you can easily see by placing your cursor on the "string" keyword and then checking your Code Definition window for the class declaration. You can also confirm this by looking at the methods of a variable of type string and noticing it has a GetEnumerator() method. All system types with this method implement IEnumerable.

    So, add System.Linq to your using directives, and write the following:

    Code:
    string str = "123hdfr456";
    str.First(c => char.IsLetter(c));
    Here, I'm using a lambda expression. You can also use a delegate function to bring back our good ol' and powerful C++ function predicates.

    In any case the above returns the first char, not its index. So:

    Code:
    string str = "123hdfr456";
    str.IndexOf(str.First(c => char.IsLetter(c)));
    Extension methods (the likes of First() and others) are somewhat reminders of the STL algorithms and of those STL types members implementing predicates. You can find them in each supporting type documentation, further down the page.

    You might ask: "You are obviously extracting a numeric value (based on your variable naming mechanism), why not just use the LastIndexOfAny method, and pass it a character array of all numeric characters?" My answer: "The user could input numeric characters somewhere later in the string. Hence it wouldn't work."
    I'm not sure I understand. But if you want to generate a string with just the numeric characters, you can use the Where() extension method.

    Of course, nothing of this invalidates Magos answer.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I was going to suggest the linq extensions. as well. They are basically a set of extension methods you can use on any IEnumerable. In the case of a string, it's an IEnumrable<char> so you can use the extension method syntax or the full on linq query syntax. I suggest reading the C# programming guide as it will probably enlighten you on the things you though not possible with the language.
    Here to Deceive, Inveigle, Obfuscate Since 1945

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  2. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 11:43 AM
  3. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM
  4. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21