-2

I have a user input variable containing the string in the format: "domain\alias" and I need to split this in two different strings: domain and alias.

I heard somewhere about conversion of strings to literals but I don't understand how that will help me here.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Siddharth
  • 141
  • 4
  • 16
  • `doamin` --> `domain` typo? Can you show an example? – Tim Schmelter Sep 07 '16 at 12:37
  • Here is the example. and yes that was a typo. my variable is say xyz. containing a string in the form of domain\alias. now i want to split xyz in two different strings: domain and alias – Siddharth Sep 07 '16 at 12:40

2 Answers2

1

Write

var x = @"doamin\alias".Split('\\') 

it will give you an array whith contents

x[0] = "doamin"
x[1] = "alias"

if you want to get user even if domain isn't specified:

var user = x.Length == 2 ? x[1] : x[0];
var domain = x.Length == 2 ? x[0] : null;
mortb
  • 9,361
  • 3
  • 26
  • 44
  • thanks. but say I have a string in a variable xyz then the above method doesn't work..please suggest. – Siddharth Sep 07 '16 at 12:27
  • Infact, it doesn't work in the above case as well, unless and until I prefix '@' to the string. – Siddharth Sep 07 '16 at 12:32
  • 1
    Well `@"domain\alias"` is *exactly* the same as writing `"domain\\alias"`. If we put the `@` in front of the string we don't have to escape the backslash. http://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c – mortb Nov 29 '16 at 08:53
1
string input = @"domain\alias";
int inputindex=  input.IndexOf("\\");           
string domain = input.Substring(0, inputindex);
string alias = input.Substring(inputindex+1);
Hameed Syed
  • 3,939
  • 2
  • 21
  • 31