Vinci
Vinci

Reputation: 29

Remove string with starts with "/" and ends with "\" in c#

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);

Upvotes: 0

Views: 5071

Answers (5)

JohnG
JohnG

Reputation: 9479

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

Upvotes: 0

active92
active92

Reputation: 654

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, @"(?<=[/])");

Upvotes: 0

Ray Krungkaew
Ray Krungkaew

Reputation: 6977

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, "/");

Upvotes: 4

Greg Uretzky
Greg Uretzky

Reputation: 62

Simple example

string userName = @"ABC/Domain-name\Pinto";
string[] arrCompany = userName.Split('/');
Console.WriteLine(arrCompany[0] +"/"+ arrCompany[1].Split('\\')[1]);
Console.ReadLine();

Upvotes: 0

Jcl
Jcl

Reputation: 28272

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

Upvotes: 0

Related Questions