-2

i need to split a string in C# .net.

output i am getting : i:0#.f|membership|sdp950452@abctechnologies.com or i:0#.f|membership|tss954652@abctechnologies.com I need to remove i:0#.f|membership| and @abctechnologies.com from the string. out put i need is sdp950452 or tss954652

also one more string I am getting is Pawar, Jaywardhan and i need it to be jaywardhan pawar

thanks, Jay

JAY
  • 43
  • 7

2 Answers2

0

Here is code example how you can do first part with Regex and the second with Splits and Replaces:

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{

public class Program
{
    public static void Main()
    {
        //First part
        string first = "i:0#.f|membership|sdp950452@abctechnologies.com";
        string second = "i:0#.f|membership|tss954652@abctechnologies.com";
        string pattern = @"\|[A-Za-z0-9]+\@";
        Regex reg = new Regex(pattern);
        Match m1 = reg.Match(first);
        Match m2 = reg.Match(second);
        string result1 = m1.Value.Replace("|",string.Empty).Replace("@",string.Empty);
        string result2 = m2.Value.Replace("|", string.Empty).Replace("@", string.Empty);
        Console.WriteLine(result1);
        Console.WriteLine(result2);

        //Second part
        string inputString = "Pawar, Jaywardhan";
        string a = inputString.ToLower(); 
        var b = a.Split(' ');
        var result3 = b[1] + " " + b[0].Replace(",",string.Empty); 
    }
}
}
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
0

Using Linq to reduce the code lines Link to dotnetfiddle code sample

using System.Linq;
using System;

public class Program
{
    public static void Main()
    {
        //Extract email
        string a = "i:0#.f|membership|sdp950452@abctechnologies.com";
        string s = a.Split('|').Where(splitted => splitted.Contains("@")).FirstOrDefault().Split('@').First();
        Console.WriteLine(s);       

        //Format Name
        string name = "Pawar, Jaywardhan";
        string formatted = String.Join(" ",name.Split(',').Reverse()).ToLower().TrimStart().TrimEnd();
        Console.WriteLine(formatted);
    }
}
Venkatesh Muniyandi
  • 5,132
  • 2
  • 37
  • 40