-3

How to find the second part of the string using substring in c#

I have follwwing string:

string str = "George Micahel R - 412352434";

I wanted the above string as two parts.Before "-" and after "-"

I have my first part using following code:

 string firstPart = str.Substring(0,str.IndexOf(" - "));

When I am trying for the second part using the following code it gives me an error.

 string secondPart = str.Substring(str.IndexOf(" - "), Len(str);

How to get the second part(only number)?

Thanks in advance.

Santosh
  • 2,355
  • 10
  • 41
  • 64
  • 2
    Whats wrong with `string.Split`? – Igor Sep 25 '18 at 20:48
  • 1
    There is nothing named `Len` in c# but you could use `str.Length` if you want the length. – Igor Sep 25 '18 at 20:48
  • 1
    your second example has a syntax error, it's missing a bracket before the `;`. And there's no built-in `Len` method. Also the length value you're using now will result in an OutOfRange exception. You might want to [look up](https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.7.2#System_String_Substring_System_Int32_System_Int32_) exactly how that second value is used. But yeah, just use string.split() tbh. https://learn.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.7.2#methods – ADyson Sep 25 '18 at 20:49
  • 1
    If you want to _split_ on a character, there is no better way than [`string.Split()`](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.7.2) – maccettura Sep 25 '18 at 20:50
  • 1
    `string[] person = str.Split('-');` then you can get the phone number using `person[1];` – max Sep 25 '18 at 20:51
  • 1
    Along with what has been said before, to use `Substring` to get the string from partway through to the end, use `str.Substring(str.IndexOf("-"), str.Length - str.IndexOf("-"))`. The second argument is the length (number of characters after start index) to grab, not the stop index. This means you subtract the start index from the length to get the number of characters to the end of the string. – St. Pat Sep 25 '18 at 20:52
  • 4
    You asked yesterday a question ([here](https://stackoverflow.com/questions/52485747/split-a-string-into-string-array-in-c-sharp)) which indicates you already know `string.Split`. Seems you have forgotten it one day later. Weird. (It is fine example in my opinion about showing that just throwing answers with prepared copy-pasta code snippets at the asking person can actually reduce the likelihood of them learning something...) –  Sep 25 '18 at 20:53

2 Answers2

4

Simply use split function:

var splittedArray = str.Split('-');
var leftPart = splittedArray[0];
var rightPart = splittedArray[1];
maccettura
  • 10,514
  • 3
  • 28
  • 35
BehiAlav
  • 178
  • 7
3

Use the Split() method instead.

string str = "George Micahel R - 412352434";
string[] words = str.Split('-');
Console.WriteLine(words[0].Trim()); # George Micahel R
Console.WriteLine(words[1].Trim()); # 412352434
maccettura
  • 10,514
  • 3
  • 28
  • 35
Antebios
  • 1,681
  • 1
  • 13
  • 22
  • 2
    there's no point using a "code snippet" here - they can only run HTML/JavaScript examples – ADyson Sep 25 '18 at 21:02
  • I was having a heck of a problem properly inserting formatted code. I just said 'screw it' just so it would look good. What button should have I used? – Antebios Sep 25 '18 at 21:46
  • You can use Ctrl+K to format a highlighted section of text as code, or the button in the little editor GUI looks like `{}`. There's a help section about the editor somewhere on the site I think. – ADyson Sep 25 '18 at 21:53