C#基础知识学习(2)string类中的方法
1.Compare 比较字符串
public static void Main() { string x = "nihao"; string y = "nihao ma";结果:-1 //2.string x = "nihao ma"; //string y = "nihao";结果: 1 //3.string x = "nihao"; //string y = "nihao";结果: 0 //4.string x = "niliu"; //string y = "nihao";结果: 1 int result = string.Compare(x,y); Console.WriteLine("结果:{0}",result); Console.ReadKey(); }string x="你好";string y=",欢迎你";string z=string.Concat(x,y);string dest = "Hello world";string source = "Goodbye China";char[] destArray = dest.ToCharArray();//将dest变成字符数组source.CopyTo(8, destArray, 6, 5);//从source的第8个字符起复制5个字符并从destArray的第6个位置开始放dest = new string(destArray);//这时dest为"Hello China"输出结果是:Hello China
- intIndexOf(charvalue)
- intIndexOf(charvalue,intstartIndex)
- intIndexOf(charvalue,intstartIndex,intcount)
定位字符串:
- intIndexOf(stringvalue)
- intIndexOf(stringvalue,intstartIndex)
- intIndexOf(stringvalue,intstartIndex,intcount)
在上述重载形式中,其参数含义如下:
Value:待定位的字符或者子串。
startIndex:在总串中开始搜索的起始位置。
Count:在总串中从起始位置开始搜索的字符数。
String str1 = "hello world"; String str2 = "abcd"; int x = str1.IndexOf("o"); Console.WriteLine("结果是{0}",x); String str1 = "hello world"; String str2 = "abcd"; int x = str1.IndexOf("o"); int y = str1.IndexOf("o",5); Console.WriteLine("结果是{0},定索引位置的索引结果是{1}",x,y); Console.ReadKey(); String str1 = "hello world or happy you "; int z = str1.IndexOf("o",10,4); Console.WriteLine("倒序索引结果{0}",z);String str1 = "hello world or happy you "; char[] b = { 'e', 'o', 'l' }; int a = str1.IndexOfAny(b,5,15); Console.WriteLine("结果是{0}}",a);String str1 = "hello world or happy you "; String str2 = "abcd"; string str3 = str1.Insert(2, str2); Console.WriteLine("insert功能插入结果{0}",str3); string[] str5 = {"fsa","fasdf","fsaf"}; string str4 = string.Join("/", str5); Console.WriteLine("Join功能结果{0}", str4); string str1 = "hello world"; char str2='a'; string str3 = str1.PadLeft(12,str2); Console.WriteLine("PadLeft功能插入结果{0}", str3);string str1 = "hello world"; char str2='a'; string str3 = str1.PadRight(12,str2); Console.WriteLine("PadLeft功能插入结果{0}", str3);public string Replace(char oldChar, char newChar);
public string Replace(string oldValue, string newValue);
string str1 = "hello world"; str1 = str1.Replace("d", "d!"); Console.WriteLine("Replace功能插入结果{0}", str1); string str1 = "hello world";string[] str2 = str1.Split('w'); string str1 = "hello world"; string str2 = str1.Split('w')[0];Console.WriteLine("分割数据{0}",str2);string str1 = "changed";string str4 = str1.Remove(1, 2); string s = "Hello C# World!"; string s1=s.Substring(3); Console.WriteLine(s1);String.Substring (Int32, Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。
string s = "Hello C# World!"; string s1=s.Substring(3,2); Console.WriteLine(s1);C#基础知识学习(2)string类中的方法的更多相关文章
- String类中intern方法的原理分析
一,前言 昨天简单整理了JVM内存分配和String类常用方法,遇到了String中的intern()方法.本来想一并总结起来,但是intern方法还涉及到JDK版本的问题,内容也相对较多,所以今 ...
- 2019.4.1今日一练String类中的方法
package com.pjc.objects; replaceAll()方法的理解引出正则表达式import java.util.regex.Patte ...
- (Object String 类中的方法练习)
package com.zs.demo1; public class Demo1 { public static void main(String[] args) { fun1(); fun2(); ...
- 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!
split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj 必选项.要被分解的 ...
- String类中toCharArray()方法的用法
该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符 eg: public class class6_3 { public static void main(String arg ...
- C#基础知识系列三(类和结构体、String和StringBuilder、equals和==)
前言 这一节主要来了解一下类和结构体之间的异同点.以及针对String和StringBuilder的用法.equals和==,其实可以看出很多地方都用到了上一节的值类型和引用类型.堆栈和装箱拆箱操作吧 ...
- String类中的常用方法
String类 一.转换成String方法 1.public String(); 空参构造 初始化一个新创建的 String 对象,使其表示一个空字符序列 2.public String(byte[] ...
- Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
1. 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再改变它的状态,那么这个对 ...
- Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用
1. Arrays工具类中asList()方法的使用 public static <T> List<T> asList(T... a): 把数组转成集合 注意事项: 虽然可以把 ...
随机推荐
- stormzhangB站直播之总结
此文转自个人微信公众号,原链接为:https://mp.weixin.qq.com/s?__biz=MzUxODk0ODQ3Ng==&mid=2247484313&idx=1& ...
- 关于爬虫的日常复习(3)—— request库
- scala 对一个数组分组操作
通常我们有一些需求,对一个数组两两进行翻转,通常就涉及到奇数偶数,否则就会出现数组index异常了,所以我们该怎么办呢? 虽然是一个入门级问题,但是我还是觉得这是一个很有意思的题目,因此写了一个对于通 ...
- 搞定SpringBoot多数据源(3):参数化变更源
目录 1. 引言 2. 参数化变更源说明 2.1 解决思路 2.2 流程说明 3. 实现参数化变更源 3.1 改造动态数据源 3.1.1 动态数据源添加功能 3.1.2 动态数据源配置 3.2 添加数 ...
- spring boot配置spring-data-jpa的时候报错CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager f ...
- xlwings excel(三)
App相当于Excel程序,Book相当于工作簿.N个Excel程序则由apps表示,N个工作簿由books表示. 对工作簿的操作 #导入xlwings模块 import xlwings as xw ...
- 共轭先验(conjugate prior)
共轭是贝叶斯理论中的一个概念,一般共轭要说是一个先验分布与似然函数共轭: 那么就从贝叶斯理论中的先验概率,后验概率以及似然函数说起: 在概率论中有一个条件概率公式,有两个变量第一个是A,第二个是B , ...
- springcloud 依赖版本问题
SpringCloud 版本: 版本名称 版本 Finchley snapshot版 Edgware snapshot版 Dalston SR1 当前最新稳定版本 Camden SR7 稳定版本 Br ...
- redis 常用命令行
一.key 相关: ()redis允许模糊查询 key(keys *) 有3个通配符 *.?.[] ()randomkey:返回随机key ()type key:返回key存储的类型 ()exists ...
- 三、通过 FactoryBean 来配置bean
一般情况下,Spring 通过反射机制利用 <bean> 的 class 属性指定实现类实例化 Bean ,在某些情况下,实例化 Bean 过程比较复杂,如果按照传统的方式,则需要在 &l ...