.length 字符串长度
.equals 比较字符串
.equalIgnoreCase 比较字符串不区别大小写
.charAt 获取字符串指定下标位置的字符
.contains 判断字符串内是否包含某字符串
.endsWith 判断字符串是不是以某字符串结尾
.startsWith 判断字符串是不是以某字符串开头
.trim 去除字符串前后的空格,不包括字符串内的
.substring 获取字符串的从制定下标开始的字符串 public class TestStr06 {
public static void main(String[] args) {
String s="helloworld";
System.out.println("============1长度===================");
System.out.println("长度:"+s.length()); System.out.println("============2字符串内容的比较===================");
System.out.println(s.equals("Hello"));
System.out.println(s.equalsIgnoreCase("HelloWOrld")); System.out.println("============3指定位置的字符===================");
System.out.println("字符d:"+s.charAt(s.length()-1)); System.out.println("============4判断是否包含===================");
System.out.println(s.contains("hehe")); System.out.println("============5 以XXX结尾===================");
System.out.println("是否以\"ld\"结尾:"+s.endsWith("ld")); System.out.println("============6 以XXX开头===================");
System.out.println("是否以\"he\"开头:"+s.startsWith("he")); System.out.println("============7 过滤2边的空格===================");
// 3个位置都是2个空格
s=" hello world ";
System.out.println("过滤前的:"+s);
System.out.println("过滤前的长度:"+s.length()); System.out.println("过滤后的:"+s.trim());
System.out.println("过滤后的长度:"+s.trim().length()); System.out.println("============8 截取子串===================");
s="helloworld";
//[2,4) 左闭右开 ,[2,3]
System.out.println("截取\"ll\":"+s.substring(2, 4));
//[2,6) [from, to) 截取的字符个数= to-from ,from是起始下标
System.out.println("截取\"llow\":"+s.substring(2, 6));
// from : 从from到最后
System.out.println("截取\"w到最后\":"+s.substring(5));
System.out.println("截取\"w到最后\":"+s.substring(5,s.length()));
}
}



StringBuffer:线程安全的,效率不高,适用于多线程
StringBuilder:线程不安全的,效率高,适用于单线程   ,如果对线程安全没有要求,优先选用StringBuilder
/**
* StringBuffer/StringBuilder: 用法基本相同 ,可变字符序列
* 构造
* @author Administrator
*
*/
public class TestStr05 {
public static void main(String[] args) {
System.out.println("============1==============");
//无参构造 默认长度可以存16个字符
StringBuffer sbu = new StringBuffer();
//append()可以写多个,没有限制
sbu.append("helloworldhelloworld")
.append("a")
.append("b")
.append("c");
System.out.println("sbu="+sbu); System.out.println("============2==============");
// 10:指的初始容量 ,初始的字符个数
sbu = new StringBuffer(10);
sbu.append("helloworldhelloworld")
.append("a")
.append("b")
.append("c");
System.out.println("sbu="+sbu); System.out.println("============3==============");
// StringBuffer(String str)
String s1="today";
sbu = new StringBuffer(s1);
System.out.println("sbu="+sbu);
}
}

 



.indexOf 获取字符串中指定元素的下标索引

.lastIndexOf 获取字符串中指定元素最后一次出现的下标索引

.replaceAll 替换全部的某元素

.replaceFirst 替换首次出现的某字符

int num = Integer.parseInt(s) 将字符串装换成int型

String result=String.valueOf(num3); 将其他类型转换为字符串

/**
* java.lang.NumberFormatException: For input string: "112" 数据格式异常
* @author Administrator
*
*/
public class TestStr07 {
public static void main(String[] args) {
String s="helloworld";
System.out.println("============1索引位置===================");
//找不到 返回-1
System.out.println("l的下标位置:"+s.indexOf("l")); //
System.out.println("l的最后一次出现下标位置:"+s.lastIndexOf("l")); //
System.out.println("KK的下标位置:"+s.indexOf("KK")); System.out.println("============2 替换===================");
System.out.println("替换掉所有的l:"+s.replaceAll("l", "*")); //he**owor*d
System.out.println("替换首个l:"+s.replaceFirst("l", "*")); //he*loworld System.out.println("替换掉所有的小写字母:"+s.replaceAll("[a-z]", "*")); //**********
System.out.println("替换掉所有的3个小写字母:"+s.replaceAll("[a-z]{3}", "*")); //***d
System.out.println("替换掉所有的3个小写字母:"+s.replaceAll("[a-z]{4}", "*")); //**ld System.out.println("============3字符串转整数===================");
s="123";
int num=Integer.parseInt(s);
System.out.println("字符串转换成int整数:"+num); s="1110";
// s="112";
//2进制的“1110” 转成10进制
num=Integer.parseInt(s,2);
System.out.println("字符串转换成int整数:"+num); s="173";
num=Integer.parseInt(s,8);
//8进制 64+56+3=123
System.out.println("字符串转换成int整数:"+num); s="1e";
num=Integer.parseInt(s,16);
//16进制 16+14=30
System.out.println("字符串转换成int整数:"+num); System.out.println("============3字符串转浮点数===================");
s="123.33";
double num2 = Double.parseDouble(s);
System.out.println("字符串转换成double浮点数:"+num2);
float num3 = Float.parseFloat(s);
System.out.println("字符串转换成float浮点数:"+num3); System.out.println("============4其他类型转换成String字符串===================");
String result=String.valueOf(num3);
System.out.println("float---》String:"+result);
}
}
 

String字符串相关操作的更多相关文章

  1. python字符串、字符串处理函数及字符串相关操作

    python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...

  2. Python_字符串相关操作

    1.字符串切片操作: str1='hello word' startIndex=0 #开始索引位置 endIndex=5 #结束索引位置+1 step=2 #步长 print(str1[startIn ...

  3. Python 字符串相关操作

    # 1 * 重复输出字符串 print('hello'*2) # 2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表 print('helloworld'[2: ...

  4. 初学Python——字符串相关操作

    基本字符串操作 Pyhton中字符串的格式化输出在前面已经总结了,接下来介绍一些常用的字符串操作 先定义一个字符变量,以下的操作都以此为例: name=" my name is china ...

  5. Swift3.0字符串相关操作

    以下有关字符串的常用操作都可直接复制到Xcode中进行验证,如发现错误,请在评论区留言指正! 1.字符串的定义 var str1="hello, swift." //字符串变量 相 ...

  6. 一:redis 的string类型 - 相关操作

    *redisclient使用: =============一类:string的方法================ 介绍:string是redis的最简单类型,一个key相应一个value,strin ...

  7. [ES6系列-05]字符串相关操作更方便

    [原创] 码路工人 Coder-Power 大家好,这里是码路工人有力量,我是码路工人,你们是力量. github-pages 博客园cnblogs 今天的内容是,关于 ES6 JavaScript ...

  8. 4.String字符串类型操作

    String类型操作 1.set key value 设置key对应的值为string类型的value  2.mset key1 value1 … keyN valueN 一次设置多个key的值 3. ...

  9. Python3中的字符串相关操作

    Python3的字符串操作相关函数详解 字符串内建函数 1. capitalize() 将字符串中的第一个字符转换成大写,其他字母变成小写.例: >>> "hello Wo ...

随机推荐

  1. FTP服务FileZilla Server上传提示550 Permission denied

    原文地址:https://help.aliyun.com/knowledge_detail/5989224.html 相关文章 1.filezilla通过root账户远程连接管理ubuntu serv ...

  2. learning scala akka tell pattern(二)

    package com.example import akka.actor._ object Tutorial_02_Tell_Pattern extends App { println(" ...

  3. 完美兼容IE10以下所有版本

    IE一直是个恶心东西 各种不支持 现在发现了个好东西可以兼容ie10以下所有浏览器 <!--[if lte IE 9]><script>window.location.href ...

  4. IIS 站点配置文件

    IIS 站点配置文件  C:/Windows/System32/inetsrv/config/applicationHost.config 配置文件示例: <system.application ...

  5. NLP算法工程师(实习生)面试总结(持续更新)

    爱奇艺 1. attention<attention is all you need> 2. 相关项目 3. 逻辑回归 4. linux的指令(重命名文件,vim替换,vim指向文章最末行 ...

  6. 第03组 团队git现场编程实战

    1.组员职责分工 张逸杰:复制监督整个编程任务的进程以及协助组员编程 黄智锋.刘汪洋:负责UI设计 苏凯婷.鲍冰如:爬取数据并负责测评出福州最受欢迎的商圈 陈荣杰.杨锦镔:爬取数据并负责测评出福州人均 ...

  7. 【转】Java 8新特性(四):新的时间和日期API

    Java 8另一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期. 在介绍本篇文章内容之前,我们先来讨论Ja ...

  8. python下浏览器静默运行驱动

    此处以chromdriver为例,放置driver路径问题参看上一篇问题.和java处理差不多,python实现静默运行方式如下 首先解答为什么进行静默运行? 我们在本地一般便于调试可以用GUI界面运 ...

  9. vim配置无插件

    其实,vim插件会影响编辑器的启动速度,虽然有些插件影响不大,我依然觉得不够,其实通过简易的状态栏,可以显示必要的信息,能自定义颜色和背景甚至透明就足够了. 一.自定义状态栏其实以下内容可以写在一行上 ...

  10. 学习opencv(1)

    目录 CV_8UC3 Scalar--颜色赋值 using namespace cv找不到命名空间 waitKey() getTickCount() 引用 CV_8UC3 a) 存放单通道图像中像素: ...