python剑指offer替换空格_《剑指Offer》字符串 替换空格

//str.replace(char oldChar,char newChar);用字符newChar替换oldChar;返回一个新的字符串

public class Solution {

public String replaceSpace(StringBuffer str) {

String s1 = new String(str);

String s2 = s1.replace(" ","%20");

return s2;

}

}

字符串处理:

1、创建字符串

Java中字符串字面量必须包含在一对双括号“”内。

声明字符串变量:

String str = new String("abc");

String str = "abc"

2、连接字符串

package eight;

//连接符 +,连接字符串

public class Link {

public static void main(String[] args){

/*int booktime = 4;

String reading = "I am reading  for ";

String time = " hours a day";

System.out.println(reading + booktime + time);*/

System.out.println("I like "+

"coding");

}

}

//I like coding

3、判断字符串是否相等

package eight;

//str.equals(str1);比较两个字符内容是否相等

public class equalsOr {

public static void main(String[] args){

String s1 = new String("abx");

String s2 = new String("ABX");

String s3 = new String("abx");

boolean b1 = s1.equals(s2);

boolean b2 = s1.equals(s3);

boolean b3 = (s1 == s3);//其实是在比较两者在内存中的地址

boolean b4 = s1.equalsIgnoreCase(s2);//不区分大小写

System.out.println(b1);

System.out.println(b2);

System.out.println(b3);

System.out.println(b4);

}

}

/*false

true

false

true*/

4、字符串比较

package eight;

//str.compareTo(str1);比较两个字符大小

public class CompareTo {

public static void main(String[] args){

String s1 = new String("a");

String s2 = new String("b");

String s3 = new String("a");

int i = s1.compareTo(s2);

int j = s2.compareTo(s1);

int k = s3.compareTo(s1);

System.out.println(i);//s1与s2比较,在其前面,输出一个负数

System.out.println(j);//s2与s1比较,在其前面,输出一个正数

System.out.println(k);//输出0

}

}

//-1

//1

//0

5、String类的常用方法

<1> length()

package eight;

//str.length(); String类的length()方法,求字符长度

public class Length {

public static void main(String[] args){

String s = "hello";

int i = s.length();

System.out.println(i);

}

}

//5

<2> indexOf()/lastIndexOf()

package eight;

//查找字符str.indexOf(substr);返回所查找字符首次出现的位置

//str.lastIndexOf(substr);返回字符最后一次出现的位置

//包括空格,str下标从0开始

//str.indexOf(String str, int fromIndex)

public class IndexOfLastIndexOf {

public static void main(String args[]){

String str = "I will be great";

int index1 = str.indexOf("l");

int index2 = str.lastIndexOf("l");

System.out.println("I在字符串中首次出现的位置是" + index1);

System.out.println("I在字符串中最后出现的位置是" + index2);

}

}

/*I在字符串中首次出现的位置是4

I在字符串中最后出现的位置是5*/

<3>charAt()

package eight;

//str.charAt(int index);返回索引位置index的字符,字符串从0开始

public class charAt {

public static void main(String[] args){

String str = "Hello Job";

char x = str.charAt(6);

System.out.println(x);

}

}

//J

<4>trim()

package eight;

//str.trim();去掉前导和尾部空白,空白包括空格和制表符

public class trim {

public static void main(String[] args){

String s1 = "  Great    Me    ";

String s2 = s1.trim();

System.out.println(s2);

}

}

//Great    Me

<5>subString()

package eight;

//返回从beginIndex开始向后的子字符串 str.substring(int beginIndex);

//返回从beginIndex(包括)到endIndex(不包括)的子字符串

public class substring {

public static void main(String[] args){

String s1 = new String("Hello Me");

String s2 = s1.substring(2);

System.out.println(s2);

String s3 = s1.substring(3,5);//包括3不包括5

System.out.println(s3);

}

}

/*llo Me

lo

*/

<6>split()

package eight;

//str.split(String sign);根据给定的分隔符sign分割字符串

//str.split(sign,int limit);根据分隔符和限定分割次数limit分割字符串

public class split {

public static void main(String[] args){

String s1 = new String("abc,def,ghi,jkl");

String[] newstr = s1.split(",");

for(int i = 0;i 

System.out.println(newstr[i]);

}

String[] s2 = s1.split(",", 2);//以,分割两次

for(int j = 0;j 

System.out.println(s2[j]);

}

}

}

/*abc

def

ghi

jkl

abc

def,ghi,jkl*/

<7>replace()

package eight;

//str.replace(char oldChar,char newChar);用字符newChar替换oldChar;返回一个新的字符串

public class replace {

public static void main(String[] args){

String str = "address is ";

String newStr = str.replace(" ", "A");

System.out.println(newStr);

}

}

///addressAisA

<8>toLowerCase()/toUpperCase()

package eight;

//str.toLowerCase();将字符串中的字符转换成小写

//str.toUpperCase();转换成大写

//字符串中数字和非字符不受影响

public class toUpperCaseToLowerCase {

public static void main(String[] args){

String s1 = "I want a great job";

String s2 = s1.toLowerCase();

String s3 = s1.toUpperCase();

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

}

}

/*I want a great job

i want a great job

I WANT A GREAT JOB*/

<9>startsWith()/endsWith()

package eight;

//str.startsWith(String prefix);判断字符串str是否以prefix开头

//str.endsWith(String suffix);判断str是否以suffix结尾

public class startsWithendsWith {

public static void main(String[] args){

String s1 = "123456";

boolean b1 = s1.startsWith("13");

boolean b2 = s1.endsWith("56");

System.out.println(b1);

System.out.println(b2);

}

}

/*false

true*/

6、练习/反转字符/charAt()

package eight;

public class stringReverse {

static String text = "reippah eb lliw I";

public static void main(String[] args){

String s1 = charAtReverse();

System.out.println(s1);

}

private static String charAtReverse(){

String newStr = "";

int len = text.length();

for(int i = len-1;i >= 0;i--){

newStr += text.charAt(i);

}

return newStr;

}

}// I will happier

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值