5
string test = " Puerto Rico 123 " ;

I would like to obtain only "Puerto Rico". The idea is that instead of 123 it can be any number. How can I achieve this?

Jim G.
  • 15,141
  • 22
  • 103
  • 166
Florin M.
  • 2,159
  • 4
  • 39
  • 97
  • Can you provide more examples of input and output? It seems you are trimming spaces, it's not clear (or lets say it's *broad*) which pattern you are using to obtain result. – Sinatr Jan 18 '17 at 12:10
  • 1
    Possible duplicate of [Find index of number from a string in C#](http://stackoverflow.com/questions/3732808/find-index-of-number-from-a-string-in-c-sharp) – PaulF Jan 18 '17 at 12:12

5 Answers5

8

I suggest using regular expressions which is quite easy in the context: get all the non-digit [^0-9]* characters from the beginning ^ of the string:

string test = " Puerto Rico 123 ";

string result = Regex.Match(test, @"^[^0-9]*").Value;

Linq is an alrernative:

string result = string.Concat(test.TakeWhile(c => c < '0' || c > '9'));

In case you want to trim the leading and trailing spaces (and have "Puerto Rico" as the answer, not " Puerto Rico "), just add .Trim(), e.g.:

string result = Regex.Match(test, @"^[^0-9]*").Value.Trim();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • You can also call `Trim` on `Value` to remove the leading and trailing spaces. – Abion47 Jan 18 '17 at 12:16
  • @Abion47: Thank you! I see: since the question suggests `"Puerto Rico"` - trimmed string - as the answer, final `.Trim()` can well be mandatory. – Dmitry Bychenko Jan 18 '17 at 12:22
  • Will fail when theres a number in the Puerto Rico part – Kees Jan 18 '17 at 12:27
  • @Kees de Wit: It won't fail; in case `"Puerto 123 Rico"` the outcome `"Puerto "` is an *expected behaviour*: "Split string *before* any *first number*". And the "string before any first number" is `"Puerto "` while the "first number" is `123` – Dmitry Bychenko Jan 18 '17 at 12:30
  • If the value would be Pu3rto Rico then it would fail – Kees Jan 18 '17 at 12:32
  • @KeesdeWit: With Pu3rto Rico the expected result is once again "Pu" due to the OP wanting the string before the first number. . . – ThatChris Jan 18 '17 at 12:33
  • 1
    @Kees de Wit: on `"Pu3rto Rico"` the outcome is `"Pu"` and that's the expected behaviour – Dmitry Bychenko Jan 18 '17 at 12:33
  • @DmitryBychenko Yea, this guy is just absurd, he downvoted my answer for the same reason... – msmolcic Jan 18 '17 at 12:39
3

You could do it with Regex as suggested, but you can try with this one as well:

var str = " Puerto Rico 123 ";
var firstDigit = str.FirstOrDefault(c => char.IsDigit(c));

if (firstDigit != default(char))
{
    var substring = str.Substring(0, str.IndexOf(firstDigit));
}

It returns the value of the first digit in your string if there is any, or '\0' if there is no digit in your string. After that, we check if first digit is anything other than '\0', if so, we take substring of the initial string from the beginning until the first occurrence of the digit in your string.

msmolcic
  • 6,407
  • 8
  • 32
  • 56
  • Will fail when theres a number in the Puerto Rico part – Kees Jan 18 '17 at 12:29
  • 1
    @KeesdeWit Question is, split string before any first number, this does exactly that. Doesn't it? :) – msmolcic Jan 18 '17 at 12:30
  • No thats not the question. Here's the question "I would like to obtain only "Puerto Rico". The idea is that instead of 123 it can be any number. How can I achieve this?" You see anything explicitly about splitting before the first number? – Kees Jan 18 '17 at 12:33
2

Simple Trim would do, IF the number would only appear on the END of the string:

var result = "Puerto Rico 123".TrimEnd(" 1234567890".ToCharArray());

or

  string source ="Puerto Rico 123";
  char[] toBeRemoved = " 1234567890".ToCharArray(); <-- note the whitespace...
  string result = source.TrimEnd(toBeRemoved); <-- remove the chars at the end     of the source
Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
  • Same as my answer – Kees Jan 18 '17 at 12:28
  • @Kees de Wit You just edited your answer (that was wrong by the way) and copied this one instead. What is you goal??? – Bidou Jan 18 '17 at 12:31
  • I only replaced the init of the char array to your method. In fact we have the same answer. – Kees Jan 18 '17 at 12:34
  • It's a best and consize solution in case the input has just *one trailing number* (like `"Puerto Rico 123"` in the question); however, it doesn't work if a number can appear somewhere in the *middle* (e.g. `"Puerto Rico 123 Alas!"`). The question is vague one, and this reading of it is possible (+1), but, please, mention your vision in your answer. – Dmitry Bychenko Jan 18 '17 at 12:45
0

You can use the Regex like this:

Regex MyRegex = new Regex("[^a-z A-Z.]", RegexOptions.IgnoreCase);
string s = MyRegex.Replace(@" Puerto Rico 123 ", @"");
Console.WriteLine(s);
Afnan Ahmad
  • 2,492
  • 4
  • 24
  • 44
-1

You can do that with a Regex like this:

Match match = Regex.Match("Puerto Rico 123", @"^.*?(?=\d)");
string text = match.Value;
Bidou
  • 7,378
  • 9
  • 47
  • 70