达内课程-字符串相关方法(上)
来源:互联网 发布:加内特知乎 编辑:程序博客网 时间:2024/05/16 14:42
charAt(int index)
作用:得到指定下标位置的单个字符
s = “abc”;
char c = charAt(2);
toUpperCase()
将字符串大写并赋值给一个变量
s = s.toUpperCase();
print(s);
toLowerCase()
将字符串小写并赋值给一个变量
length()
长度
compareTo(String anotherString)
按字符编码顺序比较字符串大小
当前字符串大,返回正数
当前字符串小,返回负数
大小相同,返回0
a.compareTo(b)
compareToIgnoreCase(String str)
忽略大小写与另一字符串按编码顺序比较大小
startsWith(String s)
是不是以s为前缀
s = “http://www.baidu.com“;
boolean b = s.starsWith(“http://”);
endsWith(String suffix)
是不是以s为后缀结尾
equals(Object object)
比较字符内容
equalsIgnoreCase(String anotherString)
忽略大小写比较字符串内容是否相同
indexOf(String s)
返回第一次找到的子串位置
s = “abc abc abc”;
int index = s.indexOf(“bc”);//返回值1
int index = s.indexOf(“bc”,2);//从2位置向后找,返回值5
int index = s.lastIndexOf(“bc”);//从后向前找,返回值9
不存在,返回-1
replace(char oldChar,char newChar)
将指定的字符替换为新字符
s = “abc abc abc”;
String s2 = s.replace(“a”,”-“);
System.out.println(s2);//不对原字符串进行修改,返回值“-bc -bc -bc”
subString(int a)
从位置a到结尾截取字符串
s = “abcdefg”;
String s2 = s.subString(3);
System.out.println(s2);//返回值”defg”;
String s3 = s.subString(3,5);//左闭右开,返回值”de”;
trim()
去除两端空白字符
s = ” a bc “;
s = s.trim();
System.out.println(s);//返回值”a bc”;
getBytes()
使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中
getBytes(字符编码)
使用指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中
String.valueOf(数据)
把任何数据转换成字符串
以上方法栗子
String s = " abc abc abc "; System.out.println(s);//" abc abc abc " System.out.println("2位置字符:"+s.charAt(2));//b System.out.println("变大写:"+s.toUpperCase());//" ABC ABC ABC " System.out.println("长度:"+s.length());//13 System.out.println("与xyz比较大小:"+s.compareTo(" xyz"));//负数 System.out.println("是否与abc开头:"+s.startsWith(" abc"));//true System.out.println("是否以bc 结尾:"+s.endsWith("bc "));//true System.out.println("第一个bc的起始位置:"+s.indexOf("bc"));//2 System.out.println("从第三个位置找bc:"+s.indexOf("bc",3));//6 System.out.println("从后向前找bc:"+s.lastIndexOf("bc"));//10 System.out.println("找不存在的字符:"+s.indexOf("xxxxx"));//-1 System.out.println("a替换成-:"+s.replace("a", "-"));//" -bc -bc -bc" System.out.println("截取6到结束:"+s.substring(6));//"bc abc" System.out.println("截取[6,10):"+s.substring(6,10));//"bc a" System.out.println("去除两端空白字符:"+s.trim());//"abc abc abc"
输出结果
abc abc abc
2位置字符:b
变大写: ABC ABC ABC
长度:13
与xyz比较大小:-23
是否与abc开头:true
是否以bc 结尾:true
第一个bc的起始位置:2
从第三个位置找bc:6
从后向前找bc:10
找不存在的字符:-1
a替换成-: -bc -bc -bc
截取6到结束:bc abc
截取[6,10):bc a
去除两端空白字符:abc abc abc
综合练习
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提取email名字部分"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="判断是否回文"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="翻转字符串"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>
java
public class MainActivity extends AppCompatActivity { EditText editText; Button button1; Button button2; Button button3; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initListeners(); } private void initViews() { editText = (EditText)findViewById(R.id.editText); button1 = (Button) findViewById(R.id.button1); button2 = (Button)findViewById(R.id.button2); button3 = (Button)findViewById(R.id.button3); textView = (TextView) findViewById(R.id.textView); } private void initListeners() { button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { f1(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { f2(); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { f3(); } }); } private void f1() { String email = editText.getText().toString(); int index = email.indexOf("@"); if(index == -1){ textView.setText("格式不正确"); return; } String name = email.substring(0,index); textView.setText(name); } private void f2() { String s = editText.getText().toString(); /* * abcdefedcba * i j * i j * 判断两个下标变量是否相等 * 如果相等继续比较,直到i<j;i和j没有必要重合 * */ for(int i=0,j=s.length()-1;i<j;i++,j--){ if(s.charAt(i)!=s.charAt(j)){ textView.setText("不是回文"); return; } textView.setText("是回文"); } } private void f3() { String s = editText.getText().toString(); String temp = ""; /* * abc->cba * i * i *i * 用下标i反向取到字符放入空字符串 * */ for(int i=s.length()-1;i>=0;i--){ temp += s.charAt(i); } textView.setText(temp); }}
- 达内课程-字符串相关方法(上)
- 达内课程-字符串相关方法(下)
- 达内课程-RandomAccessFile(上)
- 达内课程-流程控制语句(上)
- 达内课程-File用法(上)
- 达内课程-RandomAccessFile(下)
- 达内课程-数组
- 达内课程-Activity
- 达内课程-Calendar
- 达内课程-HashMap
- 达内课程-TreeMap
- 达内课程-PrintStream
- 达内课程-面向对象之构造方法和重载
- 达内C++培训课程
- 达内课程-运算符号
- 达内课程-各种监听器
- 达内课程-正则表达式
- 达内课程-异常Exception
- Firefly-RK3399 USB摄像头驱动及SimpleCV安装测试
- 虚拟机安装ubuntu(win10)
- C/C++常见面试题
- 2.9logistic回归中的梯度下降法
- 论文发表左下角添加作者信息
- 达内课程-字符串相关方法(上)
- 合唱队问题
- EasyUI入门
- eclipse设置doc注解 @author 显示自定义用户名
- HDU-5727Necklace-项链 全排列环+匈牙利
- 基于nginx+jwplayer的点播服务器(测试成功)
- 欢迎使用CSDN-markdown编辑器
- 计算机网络
- 未能加载调试符号 的一个可能原因