java 将String类型中的 A,B,C,D。。。J分别转换成1,2,。。10
不要replaceAll().replaceAll().replaceAll().replaceAll()...
不要replaceAll().replaceAll().replaceAll().replaceAll()
展开
2013-03-27 · 知道合伙人数码行家
关注
展开全部
public class Util {
public static void main(String[] args) {
String result = "A,B,C,D,E,F,G,H,L,M";
String[] arr = new String[result.length()+1]; //放到数组
String jieguo = "";
for(int i = 1; i <= 10; i++){
if(i == 10){
arr[i] = i+"";
}else{
arr[i] = i+",";
}
jieguo += arr[i];
}
System.out.println(jieguo);
}
}
结果:1,2,3,4,5,6,7,8,9,10
public static void main(String[] args) {
String result = "A,B,C,D,E,F,G,H,L,M";
String[] arr = new String[result.length()+1]; //放到数组
String jieguo = "";
for(int i = 1; i <= 10; i++){
if(i == 10){
arr[i] = i+"";
}else{
arr[i] = i+",";
}
jieguo += arr[i];
}
System.out.println(jieguo);
}
}
结果:1,2,3,4,5,6,7,8,9,10
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2018-05-07 · 知道合伙人互联网行家
关注
展开全部
这种题目超过10行代码就等于没写。
public static void main(String[] args) {
String str = "ABCDEFGHJ";
char start='A';
char end ='Z';
for(int i = 0 ; i < (int)(end-start+1) ;i++){
str = str.replaceAll(String.valueOf((char)(start+(char)i)), String.valueOf(i+1));
}
System.out.println(str);
}
public static void main(String[] args) {
String str = "ABCDEFGHJ";
char start='A';
char end ='Z';
for(int i = 0 ; i < (int)(end-start+1) ;i++){
str = str.replaceAll(String.valueOf((char)(start+(char)i)), String.valueOf(i+1));
}
System.out.println(str);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) {
char[] ch = {'A','B','C','D','E','F','G','H','I','J'};
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
array[i]=ch[i]-64;
System.out.print(array[i] + " , ");
}
}
array[i]=ch[i]-64;这里是实现转换 如果是小写的字母array[i]=ch[i]-96;
‘A’的ASCII码值是65 然后减去64就变成1喽~~其他同理~~
char[] ch = {'A','B','C','D','E','F','G','H','I','J'};
int[] array = new int[10];
for (int i = 0; i < 10; i++) {
array[i]=ch[i]-64;
System.out.print(array[i] + " , ");
}
}
array[i]=ch[i]-64;这里是实现转换 如果是小写的字母array[i]=ch[i]-96;
‘A’的ASCII码值是65 然后减去64就变成1喽~~其他同理~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个问题是什么意思?
要自己写方法么?case 补充完整就可以了
public class TestString {
public static void main(String[] args) {
TestString ts = new TestString();
String test = "ABdAdbaa1231123";
System.out.println(ts.replaceString(test));
}
private String replaceString(String oldStr) {
String newStr = "";
if (oldStr == null || oldStr == "") {
return null;
}else {
for (int i = 0; i < oldStr.length(); i++) {
String temp = new String(oldStr.substring(i,i+1));
newStr += bindSubStr(temp);
}
return newStr;
}
}
private String bindSubStr(String str){
char temp = str.charAt(0);
switch (temp) {
case 'A':
return "1";
case 'B':
return "2";
case 'C':
return "3";
}
return str;
}
}
要自己写方法么?case 补充完整就可以了
public class TestString {
public static void main(String[] args) {
TestString ts = new TestString();
String test = "ABdAdbaa1231123";
System.out.println(ts.replaceString(test));
}
private String replaceString(String oldStr) {
String newStr = "";
if (oldStr == null || oldStr == "") {
return null;
}else {
for (int i = 0; i < oldStr.length(); i++) {
String temp = new String(oldStr.substring(i,i+1));
newStr += bindSubStr(temp);
}
return newStr;
}
}
private String bindSubStr(String str){
char temp = str.charAt(0);
switch (temp) {
case 'A':
return "1";
case 'B':
return "2";
case 'C':
return "3";
}
return str;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果字符是以 , 号隔开的话就这样
String strs [] = str.split(",");
String str2 = "";
for(int i=0;i<strs.length;i++){
switch strs[i] {
case "A": str2 += "1,"; break;
case "B": str2 += "2,";break;
....
case "F": str2 += "9,";break;
default: str2+="10";break;
}
}
另外一种就是
for(int i=0;i<str.length();i++){
switch str.chartAt(i) {
case "A": str2 += "1,"; break;
case "B": str2 += "2,";break;
....
case "F": str2 += "9,";break;
default: str2+="10";break;
}
}
String strs [] = str.split(",");
String str2 = "";
for(int i=0;i<strs.length;i++){
switch strs[i] {
case "A": str2 += "1,"; break;
case "B": str2 += "2,";break;
....
case "F": str2 += "9,";break;
default: str2+="10";break;
}
}
另外一种就是
for(int i=0;i<str.length();i++){
switch str.chartAt(i) {
case "A": str2 += "1,"; break;
case "B": str2 += "2,";break;
....
case "F": str2 += "9,";break;
default: str2+="10";break;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询