zoukankan      html  css  js  c++  java
  • c# 常用字符串函数

    Compare // 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等

    CompareOrdinal  与Compare一样,但不考虑文化背景

    Format   格式化包含各种值的字符串和如何格式化每个值的说明符

    IndexOf //定位一个字符串在另一个字符串中第一次出现的位置

    LastIndexOf //定位一个字符串在另一个字符串中最后一次出现的位置

    PadLeft //在字符串的开头,通过添加指定的重复字符填充字符串

    string.PadLeft(10,'a');//10  //  字符串长度不足10位的用在字符串开头用'a'补充

    PadRight//在字符串的结尾,通过添加指定的重复字符填充字符串

    string.PadRight(10,'a');//10  //  字符串长度不足10位的用在字符串结尾用'a'补充

    Reaple//字符串替换

    string str="Hello World"

    str.Reaple("A","B");//把字符串str 中的A替换成B

    1.

    Length //获得字符串长度

    bytes[] by=System.Text.Encoding.Default.GetBytes("abc张");

    int bylen=by.Length;//长度为5,中文字符为2个

    2. 字符串相加

    System.Text.StringBuilder();

    System.Text.StringBuilder sw=new System.Text.StringBuilder();

    sw.Append("Hello");

    sw.Append("World");

    还是比较习惯用 "+",来实现字符串相加

    3.远程获得用户IP地址

     String user_IP = Request.ServerVariables["REMOTE_ADDR"].ToString();

    4.穿过代理服务器取远程用户真实IP地址:

    if (Request.ServerVariables["HTTP_VIA"] != null)
    {
    string user_IP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    }
    else
    {
    string user_IP = Request.ServerVariables["REMOTE_ADDR"].ToString();
    }

    5.获得链接传送的变量

    String str=Request.QueryString["变量"]; 

    6.时间

    System.DateTime ctime=new System.DateTime();

    ctime=System.DateTime.Now;

    int 年 = ctime.Year;//获得年
    int 月 = ctime.Month;//获得月
    int 日 = ctime.Day;//获得日
    int 时 = ctime.Hour;//获得时
    int 分 = ctime.Minute;//获得分
    int 秒 = ctime.Second;//获得秒
    int 毫秒 = ctime.Millisecond;//获得毫秒

    7.清楚空格

    Trim();

    8.查指定位置是否空字符

    char.IsWhiteSpce(字串变量,位数)——逻辑型 

    如: 
    string str="中国 人民"
    Response.Write(
    char.IsWhiteSpace(str,2)); //结果为:True, 第一个字符是0位,2是第三个字符

    9char.IsPunctuation('字符'--逻辑型 
    查字符是否是标点符号 
    如:Response.Write(
    char.IsPunctuation('A')); //返回:False 

    10、(int)'字符' 
    把字符转为数字,查代码点,注意是单引号。 
    如: 
    Response.Write((
    int)''); //结果为中字的代码:20013 

    11、(char)代码 
    把数字转为字符,查代码代表的字符。 
    如: 
    Response.Write((
    char)22269); //返回“国”字。

    12.IndexOf() 、LastIndexOf() 
    查找字串中指定字符或字串首次(最后一次)出现的位置,返回索引值,如: 
    str1.IndexOf(
    ""); //查找“字”在str1中的索引值(位置) 
    str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置) 
    str1.IndexOf("字串",3,2);//从str1第4个字符起,查找2个字符,查找“字串”的第一个字符在str1中的索引值(位置) 

  • 相关阅读:
    JSP基础语法:注释、Scriptlet、编译指令
    JDBC的LIKE书写规范
    AWT回顾篇
    1.五子棋预备知识
    对象的生命周期回顾篇
    学习activemq(2)写个简单的程序
    activemq in action(3)剖析JMS消息(转)
    activemq in action(1)
    学习activemq(3)
    hadhoop安装
  • 原文地址:https://www.cnblogs.com/yzhang/p/2726822.html
Copyright © 2011-2022 走看看