关于几个 字符串 操作的问题 关于几个 字符串 操作的问题 1. 怎么将ascii码转成字符2.求子串位置 ,例如 : this is a cup 如何求出 is 的首次的位置 , 返回 3 3.如何去除字符左边或右边空格4.删除子串 ,例如 :this is a cup 如何从第2个字符开始 删除 4个字符 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.char(65);2.String s = "this is a cup"; int index = s.indexOf("is");3.**.trim();4.StringBuffer sb = new StringBuffer(s); sb.delete(2, 2+4); String(byte[] bytes, String charsetName) String.indexOf()String.trim()String.substring() 2.求子串位置 ,例如 : this is a cup 如何求出 is 的首次的位置 , 返回 3 题错了,应该返回2才对3 如何去除字符左边或右边空格 String teststr = " ddsa "; char[] ch = teststr.toCharArray(); ----------------左trim()空格------------------- for(int i=0;i<ch.length;i++){ if(ch[i]!=' '){ char[] bufch = new char[ch.length-i]; System.arraycopy(ch, i, bufch, 0, ch.length-i); System.out.println(new String(bufch)); break; } } ----------------右trim()空格------------------- for(int i=ch.length-1;i>-1;i--){ if(ch[i]!=' '){ char[] bufch = new char[i]; System.arraycopy(ch, 0, bufch, 0, i); System.out.println(new String(bufch)); break; } } 应该是 char c = (char)65;----左trim----String s = " abc ";s = s.replaceAll("^ +", "");----右trim----String s = " abc ";s = s.replaceAll(" +$", ""); public static void main(String[] args) { String s = " \n abc \n"; System.out.println(rightTrim(s)); System.out.println(leftTrim(s));}public static String leftTrim(String str) { int index = 0; while (str.charAt(index) <= ' ') { index++; } return str.substring(index);}public static String rightTrim(String str) { int index = str.length() - 1; while (str.charAt(index) <= ' ') { index--; } return str.substring(0, index + 1);} String str=" 123456 ";去左空格 str=str.replaceAll("^\\s*","");去右空格 str=str.replaceAll("\\s*$",""); 散分了,慰劳深夜还在工作的同志们 我用java写的排序二叉树,谁帮我把遍历方法写出来? 关于JOptionPane.showMessageDialog 关于sqlserverJDBC问题 java文件缓存的问题 jdk1.3中怎么根据已有文件构造Image对象? 关于System.exit()问题! 谁能给我一些详细介绍WebLogic的资料,网址。急!!! 请问学JAVA用哪个编译器好? 再问关于StringBuffer对象到InputStream或其子类对象的转化!急急急急急急急急急急! 请问为什么将程序打包成jar文件后引用的图像出不来?急 请问如何获得自身程序所在的目录???
2.String s = "this is a cup";
int index = s.indexOf("is");
3.**.trim();
4.StringBuffer sb = new StringBuffer(s);
sb.delete(2, 2+4);
String.indexOf()
String.trim()
String.substring()
题错了,应该返回2才对
3 如何去除字符左边或右边空格
String teststr = " ddsa ";
char[] ch = teststr.toCharArray();
----------------左trim()空格-------------------
for(int i=0;i<ch.length;i++){
if(ch[i]!=' '){
char[] bufch = new char[ch.length-i];
System.arraycopy(ch, i, bufch, 0, ch.length-i);
System.out.println(new String(bufch));
break;
}
}
----------------右trim()空格-------------------
for(int i=ch.length-1;i>-1;i--){
if(ch[i]!=' '){
char[] bufch = new char[i];
System.arraycopy(ch, 0, bufch, 0, i);
System.out.println(new String(bufch));
break;
}
}
String s = " abc ";
s = s.replaceAll("^ +", "");----右trim----
String s = " abc ";
s = s.replaceAll(" +$", "");
String s = " \n abc \n";
System.out.println(rightTrim(s));
System.out.println(leftTrim(s));}public static String leftTrim(String str) {
int index = 0;
while (str.charAt(index) <= ' ') {
index++;
}
return str.substring(index);
}public static String rightTrim(String str) {
int index = str.length() - 1;
while (str.charAt(index) <= ' ') {
index--;
}
return str.substring(0, index + 1);
}
String str=" 123456 ";
去左空格 str=str.replaceAll("^\\s*","");
去右空格 str=str.replaceAll("\\s*$","");