【问题标题】:Regex leading and trailing whitespace in json property valuejson 属性值中的正则表达式前导和尾随空格
【发布时间】:2022-06-30 21:26:52
【问题描述】:

我想使用正则表达式删除 JSON 属性值中的前导和尾随空格。下面提到了“Mail”属性的值。

{
  "Customer": [
    {
      "ID": "4",
      "LastName": "Eli",
      "FirstName": "Cathrine",
      "Birthdate": "1900-01-01",
      "Mobile": "94770774485",
      "Mail": "     pqrs@gmail.com     ",
      "Note": "This is reference"
    }
  ]
}

【问题讨论】:

  • 你是在序列化还是反序列化?有什么理由不能只在对象表示上使用String.Trim

标签: c# json string


【解决方案1】:

您可以将String.Trim Method 用于 JSON 属性“邮件”。

using System;

public class Example
{
    public static void Main()
    {
        Console.Write("Enter your first name: ");
        string firstName = Console.ReadLine();
      
        Console.Write("Enter your middle name or initial: ");
        string middleName = Console.ReadLine();
      
        Console.Write("Enter your last name: ");
        string lastName = Console.ReadLine();
      
        Console.WriteLine();
        Console.WriteLine("You entered '{0}', '{1}', and '{2}'.", 
                        firstName, middleName, lastName);
      
        string name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " + 
                    lastName.Trim()).Trim();
        Console.WriteLine("The result is " + name + ".");

        // The following is a possible output from this example:
        //       Enter your first name:    John
        //       Enter your middle name or initial:
        //       Enter your last name:    Doe
        //       
        //       You entered '   John  ', '', and '   Doe'.
        //       The result is John Doe.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2011-06-28
    相关资源
    最近更新 更多