English 中文(简体)
如何在c#[closed]中分隔以下字符串
原标题:how can I separate the following string in c# [closed]
  • 时间:2023-07-13 15:15:47
  •  标签:
  • c#
Closed. This question needs details or clarity. It is not currently accepting answers.

想改进这个问题吗 通过编辑此文章添加详细信息并澄清问题。

Closed 9 hours ago.

我正在使用C#进行一个项目。我有以下可能的字符串:

!WTE|VESA * U:miam@contoso.com|VE * U:hasan@microsoft.com|VES

!Z CRE Forms|VESA * Dept IT Users|VS

!U:miam@contoso.com|VESA * U:hasan@microsoft.com|VESA

如何在第一个字符串中找到3个单独的ID并将以下3个字符串分开:

WTE VESA 

miam@contoso.com VE 

hasan@microsoft.com VES 

同样,我如何将以下两个字符串与第二个字符串分开:

Z CRE Forms VESA

Dept IT Users VESA

同样,字符串可能是第三行,我必须分开:

miam@contoso.com VESA

hasan@microsoft.com VESA
问题回答

尝试:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
    class Program
    {
        
        static void Main(string[] args)
        {
            string[] inputs =
            {
                "!WTE|VESA * U:miam @contoso.com | VE * U:hasan @microsoft.com | VES",
                "!Z CRE Forms | VESA * Dept IT Users| VS",
                "!U: miam @contoso.com | VESA * U:hasan @microsoft.com | VESA"
            };

            foreach(string input in inputs)
            {
                string[] splitArray = input.Substring(1).Replace("|", " ").Split(new char[] {  *  });
                foreach(string line in splitArray)
                {
                    string output = line.Trim();
                    if(output.Contains(":"))
                    {
                        output = output.Substring(output.IndexOf(":") + 1).Trim();
                    }
                    output = Regex.Replace(output, @"s+", " ");
                    output = Regex.Replace(output, @"s@", "@");
                    Console.WriteLine(output);
                }
                Console.WriteLine();

            }
            Console.ReadLine();
        }
       
     
    }

}
 

后果

WTE VESA
miam@contoso.com VE
hasan@microsoft.com VES

Z CRE Forms VESA
Dept IT Users VS

miam@contoso.com VESA
hasan@microsoft.com VESA




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签