zoukankan      html  css  js  c++  java
  • String.Split分隔字符串

    一种char分隔符

    string phrase = "The quick brown fox jumps over the lazy dog.";
    string[] words = phrase.Split(' ');
    foreach (var word in words)
    {
        System.Console.WriteLine($"<{word}>");
    }

    分隔之后的结果,去掉多余的空格

    // StringSplitOptions.RemoveEmptyEntries移除多余的空格
    string phrase = "The quick brown    fox     jumps over the lazy dog.";
    string[] words = phrase.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    foreach (var word in words)
    {
        System.Console.WriteLine($"<{word}>");
    }

    多种char分隔符

    // 使用多个分隔符
    char[] delimiterChars = { ' ', ',', '.', ':', '	' };
    string text = "one	two three:four,five six seven";
    System.Console.WriteLine($"Original text: '{text}'");
    // public String[] Split(params char[] separator);
    string[] words = text.Split(delimiterChars);
    System.Console.WriteLine($"{words.Length} words in text:");
    foreach (var word in words)
    {
        System.Console.WriteLine($"<{word}>");
    }

    多种string分隔符

    string[] separatingStrings = { "<<", "..." };
    string text = "one<<two......three<four";
    System.Console.WriteLine($"Original text: '{text}'");
    //public String[] Split(String[] separator, StringSplitOptions options);
    string[] words = text.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);
    System.Console.WriteLine($"{words.Length} substrings in text:");
    
    foreach (var word in words)
    {
        System.Console.WriteLine(word);
    }
  • 相关阅读:
    java对象和json对象之间互相转换
    SVN使用教程总结
    JVM调优总结-调优方法
    tomcat 高并发配置 与优化
    redis的主从复制配置
    Java实现Redis持久化到数据库的关键方法
    java操作redis
    SSO系统的实现
    jedisClient操作redis实现增删改查功能
    get与post
  • 原文地址:https://www.cnblogs.com/bibi-feiniaoyuan/p/split.html
Copyright © 2011-2022 走看看