当前位置: 首页 > news >正文

做网站容易吗网站建站平台排行榜

做网站容易吗,网站建站平台排行榜,简述网站建设的过程,吉林市网站建设目录 1. String类的重要性 2.常用方法 2.1字符串构造 2.2 String对象的比较 2.3字符串查找 2.4转化 2.5字符串替换 2.6字符串拆分 2.7字符串截取 2.8其他操作方法 2.9字符串的不可变性 2.10字符串修改 3. StringBuffer和StringBuilder 3.1StringBuilder的介绍 4.…目录 1. String类的重要性 2.常用方法 2.1字符串构造 2.2 String对象的比较 2.3字符串查找 2.4转化 2.5字符串替换 2.6字符串拆分 2.7字符串截取 2.8其他操作方法 2.9字符串的不可变性 2.10字符串修改 3. StringBuffer和StringBuilder 3.1StringBuilder的介绍 4.String类oj 1. String类的重要性 在C语言中已经涉及到字符串了 但是在C语言中要表示只要使用字符数组或者字符指针就可以使用标准库提供的字符串系列函数完成大部分的操作但是这种将数组和操作方法分离开的方式不符合面向对象的思想而字符串应用又非常广泛因此Java语言专门提供了String类。 2.常用方法 2.1字符串构造 String类提供的构造方式非常多常用的就以下三种 public static void main(String[] args) { // 使用常量串构造 String s1 hello bit; System.out.println(s1); // 直接newString对象 String s2 new String(hello bit); System.out.println(s1); // 使用字符数组进行构造 char[] array {h,e,l,l,o,b,i,t}; String s3 new String(array); System.out.println(s1); }【注意】 1.String是引用类型内部并不存储字符串本身 public static void main(String[] args) { // s1和s2引用的是不同对象 s1和s3引用的是同一对象 String s1 new String(hello); String s2 new String(world); String s3 s1; System.out.println(s1.length()); // 获取字符串长度---输出5 System.out.println(s1.isEmpty()); // 如果字符串长度为0返回true否则返回false } 2.在Java中“”引起来的也是String类型对象 // 打印hello字符串(String对象)的长度 System.out.println(hello.length()); 2.2 String对象的比较 字符串的比较是常见操作之一比如字符串排序。在Java中总共提供了四种方式。 1.比较是否引用同一个对象 注意对于内置类型比较的是变量中的值对于引用类型比较的是引用中的地址。 public static void main(String[] args) { int a 10; int b 20; int c 10; // 对于基本类型变量比较两个变量中存储的值是否相同 System.out.println(a b); // false System.out.println(a c); // true // 对于引用类型变量比较两个引用变量引用的是否为同一个对象 String s1 new String(hello); String s2 new String(hello); String s3 new String(world); String s4 s1; System.out.println(s1 s2); // false System.out.println(s2 s3); // false System.out.println(s1 s4); // true }2.Boolean equalsObject an Object方法按照字典序比较 字典序字符大小的比较 String类重写了父类Object中的equals方法Object中equals默认按照比较String重写equals方法后按照如下规则进行比较比如s1.equals(s2) public boolean equals(Object anObject) { // 1. 先检测this 和 anObject 是否为同一个对象比较如果是返回true if (this anObject) { return true; } // 2. 检测anObject是否为String类型的对象如果是继续比较否则返回false if (anObject instanceof String) { // 将anObject向下转型为String类型对象 String anotherString (String)anObject; int n value.length; // 3. this和anObject两个字符串的长度是否相同是继续比较否则返回false if (n anotherString.value.length) { char v1[] value; char v2[] anotherString.value; int i 0; // 4. 按照字典序从前往后逐个字符进行比较 while (n-- ! 0) { if (v1[i] ! v2[i]) return false; i; } return true; } } return false }public static void main(String[] args) { String s1 new String(hello); String s2 new String(hello); String s3 new String(Hello); // s1、s2、s3引用的是三个不同对象因此比较结果全部为false System.out.println(s1 s2); // false System.out.println(s1 s3); // false // equals比较String对象中的逐个字符 // 虽然s1与s2引用的不是同一个对象但是两个对象中放置的内容相同因此输出true // s1与s3引用的不是同一个对象而且两个对象中内容也不同因此输出false System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false }3.int compareTo(String s )方法按照字典序进行比较 与equals不同的是equals返回的是Boolean类型而compareTo返回的是int类型。具体比较方式 先按照字典次序比较大小如果不出现不等的字符直接返回这两个字符的大小差值。如果前k个字符相等k为两个字符长度最小值返回值两个字符串长度差值。 public static void main(String[] args) { String s1 new String(abc); String s2 new String(ac); String s3 new String(abc); String s4 new String(abcdef); System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1 System.out.println(s1.compareTo(s3)); // 相同输出 0 System.out.println(s1.compareTo(s4)); // 前k个字符完全相同输出长度差值 -3 }4.int compareTolgnoreCase(String str)方法与compare To 方式相同但是忽略大小写比较。 public static void main(String[] args) { String s1 new String(abc); String s2 new String(ac); String s3 new String(abc); String s4 new String(abcdef); System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1 System.out.println(s1.compareTo(s3)); // 相同输出 0 System.out.println(s1.compareTo(s4)); // 前k个字符完全相同输出长度差值 -32.3字符串查找 字符串查找也是字符串中非常常见的操作String类提供的常用查找的方法 方法功能char charAt(int index)返回index位置上字符如果index为负数或者越界抛出indexOutOfBoundsException异常int indexOf(int ch)返回ch第一次出现的位置没有返回-1int indexOf(int ch,int fromIndex) 从fromIndex位置开始找ch第一次出现的位置没有返回-1 int indexOf(String str)返回str第一次出现的位置没有返回-1int indexOf(String str,int fromIndex)从fromIndex位置开始找str第一次出现的位置没有返回-1int lastIndexOf(int ch)从后往前找返回ch第一次出现的位置没有返回-1int lastIndexOf(int ch,int fromIndex)从fromIndex位置开始找从后往前找ch第一次出现的位置没有返回-1int lastIndexOf(String str)从后往前找返回str第一次出现的位置没有返回-1int lastIndexOf(string str,int fromIndex)从fromIndex位置开始找从后往前找dstr第一次出现的位置没有返回-1 public static void main(String[] args) { String s aaabbbcccaaabbbccc; System.out.println(s.charAt(3)); // b System.out.println(s.indexOf(c)); // 6 System.out.println(s.indexOf(c, 10)); // 15 System.out.println(s.indexOf(bbb)); // 3 System.out.println(s.indexOf(bbb, 10)); // 12 System.out.println(s.lastIndexOf(c)); // 17 System.out.println(s.lastIndexOf(c, 10)); // 8 System.out.println(s.lastIndexOf(bbb)); // 12 System.out.println(s.lastIndexOf(bbb, 10)); // 3注意上述方法都是实例方法。 2.4转化 1.数字和字符串转化 public static void main(String[] args) { // 数字转字符串 String s1 String.valueOf(1234); String s2 String.valueOf(12.34); String s3 String.valueOf(true); String s4 String.valueOf(new Student(Hanmeimei, 18)); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(); // 字符串转数字 // 注意Integer、Double等是Java中的包装类型 int data1 Integer.parseInt(1234); double data2 Double.parseDouble(12.34); System.out.println(data1); System.out.println(data2); }2.大小写转换 public static void main(String[] args) { String s1 hello; String s2 HELLO; // 小写转大写 System.out.println(s1.toUpperCase()); // 大写转小写 System.out.println(s2.toLowerCase()); } 3.字符串转数组 public static void main(String[] args) { String s hello; // 字符串转数组 char[] ch s.toCharArray(); for (int i 0; i ch.length; i) { System.out.print(ch[i]); } System.out.println(); // 数组转字符串 String s2 new String(ch); System.out.println(s2); }4.格式化 public static void main(String[] args) { String s String.format(%d-%d-%d, 2019, 9,14); System.out.println(s); } 2.5字符串替换 使用一个指定的新的字符串替换掉已有的字符串数据可用的方法如下 方法功能String replace All(String regex, String replacement)替换所有的指定内容String replaceFirst(String regex,String replacement)替换几个内容 代码示例字符串的替换处理 String str helloworld ; System.out.println(str.replaceAll(l, _)); System.out.println(str.replaceFirst(l, _)); 【注意事项】由于字符串时不可变对象替换不修改当前字符串而是产生一个新的字符串。 2.6字符串拆分 可以将一个完整的字符串按照指定的分隔符划分成若干个子字符串。 可用方法如下 方法功能String[] split(String regex)将字符串全部拆分String[] split(String regex, int limit)将字符串以指定的格式拆分成limit组 代码示例实现字符串的拆分处理。 String str hello world hello bit ; String[] result str.split( ) ; // 按照空格拆分 for(String s: result) { System.out.println(s); } 代码示例字符串的部分拆分 String str hello world hello bit ; String[] result str.split( ,2) ; for(String s: result) { System.out.println(s); }代码示例拆分IP地址 String str 192.168.1.1 ; String[] result str.split(\\.) ; for(String s: result) { System.out.println(s); }【注意事项】 字符“|”“*”“”都得加上转义字符前面加上“\\”。而如果是“\”那么就得写成“\\\”。如果一个字符串中有多个分隔符可以用“|”作为连字符。 代码示例多次拆分 String str namezhangsanage18 ; String[] result str.split() ; for (int i 0; i result.length; i) { String[] temp result[i].split() ; System.out.println(temp[0] temp[1]); } 2.7字符串截取 从一个完整的字符串中截取出部分内容。可用方法如下 方法功能String substring(int beginIndex)从指定索引截取到结尾String substring(int beginIndex, int endIndex)截取部分内容 代码示例观察字符串截取 String str helloworld ; System.out.println(str.substring(5)); System.out.println(str.substring(0, 5)); 【注意事项】 索引从0开始注意前闭后开区间的写法substring(0,5)表示包含0号下标的字符不包含5号下标 2.8其他操作方法 方法功能String trim()去掉字符串中的左右空格保留中间空格String toUppperCase()字符串转大写String toLowerCase()字符串转小写 代码示例观察trim方法的使用 String str hello world ; System.out.println([str]); System.out.println([str.trim()]); trim会去掉字符串开头和结尾的空白字符空格换行制表符等。 代码示例大小写转换 String str hello%$$%#$%world 哈哈哈 ; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); 这两个函数只转换字母。 2.9字符串的不可变性 String是一种不可变对象字符串中的内容是不可改变。字符串不可呗修改是因为 1.String类在设计时就是不可改变的String类实现描述中已经说明了 String类中的字符实际保存在内部维护的value字符数组中。 2.所有涉及到可能修改字符串内容的操作都是创建一个新的对象改变的是新对象。 final修饰类表明该类不想被继承final修饰引用类型表明该引用变量不能引用其他对象但是其引用对象中的内容是可以修改的。 2.10字符串修改 注意尽量避免直接对string类型对象进行修改因为String类是不能被修改的所有的修改都会创建新的对象效率非常低下。 public static void main(String[] args) { String s hello; s world; System.out.println(s); // 输出hello world } public static void main(String[] args) { long start System.currentTimeMillis(); String s ; for(int i 0; i 10000; i){ s i; } long end System.currentTimeMillis(); System.out.println(end - start); start System.currentTimeMillis(); StringBuffer sbf new StringBuffer(); for(int i 0; i 10000; i){ sbf.append(i); } end System.currentTimeMillis(); System.out.println(end - start); start System.currentTimeMillis(); StringBuilder sbd new StringBuilder(); for(int i 0; i 10000; i){ sbd.append(i); } end System.currentTimeMillis(); System.out.println(end - start); } 可以看待在对String类进行修改的时候效率是非常慢的因此尽量避免对String类的直接需要如果要修改尽量使用StringBuffer或者StringBuilder。 3. StringBuffer和StringBuilder 3.1StringBuilder的介绍 方法功能StringBuff append(String str)在尾部追加相当于String的“”可以追加boolean,char,char[],double,float,int,Object,String,StringBuff的变量char charAt(int index)获取index位置的字符int length()获取字符串的长度int  capacity()获取底层保护字符串空间总的大小void ensureCapacity(int mininmumCapacitty) 扩容void setCharAt(int index,chae ch)将index位置的字符设置为chint indexof(String str)返回str第一次出现的位置int indexOf(String str, int fromIndex)从fromIndex位置开始查找str第一次出现的位置int lastIndexOf(String str)返回最后一次出现的Str位置int lastIndexOf(String str, int fromIndex)从fromIndex位置开始找Str最后一次出现的位置StringBuff insert(int offset,String str)在Offset位置插入八种基本类型String类型Object类型数据StringBuffer deleteCharAt(int index)删除Index位置字符StringBufffer delete(int start, int end)删除【startend】区间的字符String Buffer replace(int start,int end, String str) 将【startend】位置的字符替换成str String substring(int start)从start开始一直到末尾的字符以String的方式返回String substring(int start,int end)将【startend】范围内的字符以String的方式返回StringBuffer reverse()反转字符串String toSting()将所有字符按照String的方式返回 public static void main(String[] args) { StringBuilder sb1 new StringBuilder(hello); StringBuilder sb2 sb1; // 追加即尾插--字符、字符串、整形数字 sb1.append( ); // hello sb1.append(world); // hello world sb1.append(123); // hello world123 System.out.println(sb1); // hello world123 System.out.println(sb1 sb2); // true System.out.println(sb1.charAt(0)); // 获取0号位上的字符 h System.out.println(sb1.length()); // 获取字符串的有效长度14 System.out.println(sb1.capacity()); // 获取底层数组的总大小 sb1.setCharAt(0, H); // 设置任意位置的字符 Hello world123 sb1.insert(0, Hello world!!!); // Hello world!!!Hello world123 System.out.println(sb1); System.out.println(sb1.indexOf(Hello)); // 获取Hello第一次出现的位置 System.out.println(sb1.lastIndexOf(hello)); // 获取hello最后一次出现的位置 sb1.deleteCharAt(0); // 删除首字符 sb1.delete(0,5); // 删除[0, 5)范围内的字符 String str sb1.substring(0, 5); // 截取[0, 5)区间中的字符以String的方式返回 System.out.println(str); sb1.reverse(); // 字符串逆转 str sb1.toString(); // 将StringBuffer以String的方式返回 System.out.println(str); } 4.String类oj 1.第一次只出现一次的字符 class Solution { public int firstUniqChar(String s) { int[] count new int[256]; // 统计每个字符出现的次数 for(int i 0; i s.length(); i){ count[s.charAt(i)]; } // 找第一个只出现一次的字符 for(int i 0; i s.length(); i){ if(1 count[s.charAt(i)]){ return i; } } return -1; } } 2.最后一个单词的长度 import java.util.Scanner; public class Main{ public static void main(String[] args){ // 循环输入 Scanner sc new Scanner(System.in); while(sc.hasNext()){ // 获取一行单词 String s sc.nextLine(); // 1. 找到最后一个空格 // 2. 获取最后一个单词从最后一个空格1位置开始一直截取到末尾 // 3. 打印最后一个单词长度 int len s.substring(s.lastIndexOf( )1, s.length()).length(); System.out.println(len); } sc.close(); } } 3.检测字符串是否为回文 class Solution { public static boolean isValidChar(char ch){ if((ch a ch z) || (ch 0 ch 9)){ return true; } return false; } public boolean isPalindrome(String s) { // 将大小写统一起来 s s.toLowerCase(); int left 0, right s.length()-1; while(left right){ // 1. 从左侧找到一个有效的字符 while(left right !isValidChar(s.charAt(left))){ left; } // 2. 从右侧找一个有效的字符 while(left right !isValidChar(s.charAt(right))){ right--; } if(s.charAt(left) ! s.charAt(right)){ return false; }else{ left; right--; } } return true; } }
http://www.dnsts.com.cn/news/224061.html

相关文章:

  • 学生做兼职去哪个网站网站免费进入窗口软件有哪些
  • 专业的网站建设服务乐山建网站
  • 做线上兼职的网站手机站模板
  • 在网站建设中遇到的问题东营市东营区建设信息网
  • 网站信息推广的策略有哪些建站之星网站
  • 泉州做企业网站企业融资数据在哪查
  • 做网站首选科远网络梅河口市住房和城乡建设局网站
  • 重庆名威建设工程咨询有限公司网站做俄罗斯外贸网站
  • 软件工程考研难度最新seo快排技术qq
  • 找做防水去那个网站超市库存管理软件
  • 精准数据营销方案万秀服务不错的seo推广
  • 营业执照上有以上除网站制作WordPress tips
  • edu网站开发哪些h5网站比较好
  • 西宁市网站建设网站服务器选择
  • 如何提升网站知名度镇江网站排名优化公司
  • 网站开发怎样将信息栏到最底部wordpress调用热门标签
  • 创口贴设计网站官网天元建设集团有限公司企查查
  • 深圳住房和建设局网站登录招标网站哪个好
  • 兰州网站排名优化公司广州企业网站哪家好
  • 域名有了怎么制作网站怎么建设阿里巴巴国际网站首页
  • 网站推广六种方法网站锚点怎么做
  • 深圳需要做网站的公司有哪些惠州网站建设米普可思
  • 用ps如何做短视频网站生活服务网站建设
  • 文章内容网站系统百度收录公司网站
  • 做精彩网站分析的方向是犀牛建筑网校
  • 建设网站要准备什么站酷网官网
  • 必应网站管理员工具栾城区城乡建设局网站
  • 会同县做网站wordpress使用qq登录
  • 专业建站分销商城广州外贸网站建设 open
  • 专门做门业的网站网站如何做查询表单