0

I have a login username as a string and I would like to remove a substring from that username. e.g. username= "ABC/Domain-name\Pinto" So in this case, I want only ABC/Pinto or only "Pinto" as a output. Kindly assist me on this.

This is my current code.

var removeDomainName= userName.Substring(userName.IndexOf('/') + 1);
active92
  • 644
  • 12
  • 24
Vinci
  • 19
  • 1
  • 2
  • 7
  • If the format of input string is always the same you can use `Split('\\')` that returns an array and take the element at `[1]`. – Alessandro D'Andria Sep 19 '16 at 07:33
  • Is it guaranteed to have at most one `'/'` and one `'\\'`? If there can be more of them, what is the desired output? – grek40 Sep 19 '16 at 08:59
  • Possible duplicate of [Built-in helper to parse User.Identity.Name into Domain\Username](http://stackoverflow.com/questions/349520/built-in-helper-to-parse-user-identity-name-into-domain-username). – Sergey Vyacheslavovich Brunov Sep 19 '16 at 10:02

5 Answers5

4

I suggest you to try to use regular expression. Once you familiar with it, string manipulation will be a much easier task for you.

string input = @"ABC/Domain-name\Pinto";
string regex = @"(\/){1}(\w.*)(\\){1}";
var result = Regex.Replace(input, regex, "/");

Try it here: http://regexr.com/3e8l0

Simplified REGEX according to @grek40 comment

string input = @"ABC/Domain-name\Pinto";
string regex = @"(\/.*\\)";
var result = Regex.Replace(input, regex, "/");
Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28
  • 1
    Why do you have the `\w` in it and why so many braces and count qualifiers? Regex is the right approach, but I think your expression can be simplified. – grek40 Sep 19 '16 at 08:55
  • Having both, the verbose and the minimal approach looks fine. You already have my +1 anyway – grek40 Sep 19 '16 at 09:01
0

Simple example

string userName = @"ABC/Domain-name\Pinto";
string[] arrCompany = userName.Split('/');
Console.WriteLine(arrCompany[0] +"/"+ arrCompany[1].Split('\\')[1]);
Console.ReadLine();
Greg Uretzky
  • 120
  • 5
  • Thanks. I tried this one but the output given by this code is "Domain-name\Pinto" But I want the output "ABC/Pinto". What should I do to get this output. Thanks in advance. – Vinci Sep 19 '16 at 08:38
  • I have edit my answer, all I have change is this line Console.WriteLine(arrCompany[0] +"/"+ arrCompany[1].Split('\\')[1]); – Greg Uretzky Sep 19 '16 at 08:51
0

Try something like this.

string userName = @"ABC/Domain-name\Pinto";
int pos = userName.LastIndexOf("/") + 1;
Console.WriteLine(userName.Substring(pos, userName.Length - pos));

Another way to do is to replace all the \ with / so that it is easier to split. Then, combine them according to your need.

string userName = @"ABC/Domain-name\Pinto";
string edit = userName.Replace('\\', '/');
string[] split = edit.Split(new Char[] {'/'}); 
string final = split[0] + "/" + split[2];
Console.WriteLine(final);   

If you want to keep the backslash, you can replace

string[] split = edit.Split(new Char[] {'/'}); 

with

string[] split = Regex.Split(edit, @"(?<=[/])");
active92
  • 644
  • 12
  • 24
  • Thanks. I tried the second one but the output given by this code is "ABCPinto". Bit closer. But I want the output "ABC/Pinto". What should I do to get this output. Thanks in advance. – Vinci Sep 19 '16 at 09:02
  • just add a backslash at string final = split[0] + split[2];. please check the edited code. you can check it here https://dotnetfiddle.net/vTqbmS. also, if you read the last part, you can also get your desired output. – active92 Sep 19 '16 at 09:07
  • Hi, And what if in userName having plane value like "Admin" then the string final = split[0] + "/" + split[2]; line gives error "Index was outside the bounds of the array." – Vinci Sep 20 '16 at 10:15
  • @Vinci wrap it up with if-else statement. Check this out https://dotnetfiddle.net/f8rTZi – active92 Sep 20 '16 at 12:56
0

You can split the three parts:

var splitted = userName.Split(new char[] {'\\', '/'});

Where split[0] would be your prefix (ABC), split[1] will be your domain (Domain-name) and split[2] will be your username (Pinto)

I've made a fiddle: https://dotnetfiddle.net/zvbwZi

Alternatively, you can substring it:

To get the prefix and the username (e.g. ABC\Pinto):

var prefix = userName.Substring(0, userName.IndexOf('/'));
var user = userName.Substring(userName.LastIndexOf('\\'));
var prefixAndUser = prefix + user;

Then to get just the username (e.g. Pinto):

var userNameWithoutPrefixOrDomain = user.Substring(1);

Or if you want the forward slash (ABC/Pinto):

var prefix = userName.Substring(0, userName.IndexOf('/')+1);
var user = userName.Substring(userName.LastIndexOf('\\')+1);

In this case, user will already be your username

I've made a fiddle too: https://dotnetfiddle.net/7YL0m5

Jcl
  • 27,696
  • 5
  • 61
  • 92
0
private string ParseString(string inString)
{
  string prefix = "";
  string suffix = "";
  string[] splitStringArray;

  splitStringArray = inString.Split('/');
  prefix = splitStringArray[0];
  if (prefix != "ABC")
  {
    prefix = "";
  }
  splitStringArray = inString.Split('\\');
  suffix = splitStringArray[1];
  if (prefix != "")
  {
    //MessageBox.Show("Result: " + prefix + "/" + suffix);
    return prefix + "/" + suffix;
  }
  else
  {
    return suffix;
  }
}

Just an Example

Regular Expressions are the best rout for parsing strings

JohnG
  • 9,259
  • 2
  • 20
  • 29