zoukankan      html  css  js  c++  java
  • 5.0 String And RegularExpressions.

    String is something we are familiar with. So I only tried some of its functions.

    Let's see in detail.

    ---Compare()---

    (codes)

    (output)

    The method compares two strings, "abcd" and "ABCD", and returns -1 as the result, since "abcd" is smaller than "ABCD".

    However there is a way to do the insensitive comparing.

    (codes)

     

    (output)

    If we add true as the third parameter of the Compare() method, it will work in insensitive way, that is, it doesn't care about whether strings are upper case or not. So it returns 0 to show "abcd" and "ABCD" are equal.

    ---Split()---

    (codes)

    (output)

    This method is to split a string by delimiters passed to it.

    ---StringBuilder--- 

    String is immutable ( can't be changed ), while StringBuilder is mutable ( can be changed ).

    (codes)

    (output)

    AppendFormat() is used to update the current string by appending a string at the end of it.

    It is said to be more efficient to use StringBuilder than using String.

    ---------------------so much for simple use of String------------------------

    Now, it's time for RegularExpressions ~ ~ ~

    First, I summed up some simple metacharacters and their corresponding meanings.

    ^ : Begins a new line

    | : Or

    @ : Using verbatim string literals

    \S (capital) : Nonspace, one or more characters

    \s (lower case) : Space

    ?<...> : Set the name of  a group

    \d : One or more numbers

    Then, here comes some specific use.

    ---Regex and Split()---

    There are two ways we can utilize.

    (codes 1)

    (codes 2)

    (output for codes 1 and 2)

    The codes above are to split the string using Regex as delimiters.

    However, when I tried Regex with Split(), I found a confusing problem.

    (codes)

    As you can see, I just changed the order of strings of the Regex, and I thought it wouldn't make a difference to the output, but...

    (output)

    The real output isn't consistent with my assumption in some way. But why ...

    ---Regex and Match---

    (codes)

    (output)

    The instance of MatchCollection stores substrings that match the regular expressions. And the instance of Match refers to each substring that match rules. 

    ---Regex and Group---

    An example that has only one match found.

    (codes)

    (output)

    We can separate the match into several groups, and use Groups[...] to refer to some group we want.

    Another example  that has several matches found.

    (codes)

    (output)

    ---Regex and CaptureCollection---

    Match inherits Group, and Group inherits Capture.

    (codes)

    (output)

    As in the above example, when there are two "company"s in one Regex, Groups["company"] can only show the last "company" since the former one is covered by the latter one. In this situation, we need to use Capture to access each "company", for Capture stores all "company"s. 

    -----------------------so much---------------------

    -----------------------END & TO BE CONTINUED------------------------

  • 相关阅读:
    Android源码服务专家(申明:来源于网络)
    Android超精准计步器开发-Dylan计步(申明:来源于网路)
    eclipse中一个项目引用另一个项目的方法(申明:来源于网络)
    javaweb(二十九)——EL表达式
    javaweb学习总结(二十八)——JSTL标签库之核心标签
    javaweb(二十六)——jsp简单标签标签库开发(二)
    javaweb(二十四)——jsp传统标签开发
    javaweb(二十三)——jsp自定义标签开发入门
    javaweb(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
    javaweb(二十一)——JavaWeb的两种开发模式
  • 原文地址:https://www.cnblogs.com/lyli/p/4439825.html
Copyright © 2011-2022 走看看