java 怎么输出字符串中所有冒号前面的第一个内容 java中如何输出字符串里的每个字符

java \u600e\u4e48\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5192\u53f7\u524d\u9762\u7684\u7b2c\u4e00\u4e2a\u5185\u5bb9

public static void main(String args[]){
String a ="Bus 02.Port1: Dev1, Class=root_hub, Driver=ehci_hcd/2p, 480M"
+ "|__ Port1: Dev2, If 0, Class=hub, Driver=hub/6p, 480M"
+ "|__ Port1: Dev3, If 0, Class=print, Driver=usblp, 12M";
StringBuffer b = new StringBuffer();
b.append(a.substring(a.indexOf(".")-1, a.indexOf(".")));
int size = a.split(":").length;
for(int i=0;i<size-1;i++){
String tmp =a.split(":")[i];
b.append("-"+tmp.substring(tmp.length()-1, tmp.length()));
}
System.out.println(b.toString());
}

\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a2-1-1-1

public class Yugi{ public static void main(String[] args){ String str = "23234sdfsf"; for(int i = 0; i < str.length(); i++){ System.out.println(str.charAt(i)); } }}

这是我写的,你可以根据你的需要改一下。

/**
 * 读取字符串
 * 
 * @param input
 *            字符串
 * @param word
 *            字符
 * @param offset
 *            所在位置
 * @param count
 *            出现次数
 */
private static void readWord(String input, String word, int offset,
int count) {
offset = input.indexOf(word, offset);
if (offset != -1) {
System.out.println(word + " 在第 " + offset + "个位置出现过.");
if (offset > 0) {
System.out.println(word + "之前的字符为:"
+ input.substring(offset - 1, offset));
}
readWord(input, word, ++offset, ++count);
} else {
System.out.println(word + " 总共出现了:" + count + " 次.");
}
}

public static void main(String[] args) {
String input = "Look buddy, U got work hard and put yourself to the java, once U learned the heart of java, I can guarantee that U win.";
String word = "r";
readWord(input, word, 0, 0);
}

运行结果是:



String msg="Bus 02.Port1: Dev1, Class=root_hub, Driver=ehci_hcd/2p, 480M"
+ "|__ Port1: Dev2, If 0, Class=hub, Driver=hub/6p, 480M"
+ "|__ Port1: Dev3, If 0, Class=print, Driver=usblp, 12M";
String []sp = msg.split(":");
for (int i=0; i<sp.length - 1; i++) {
System.out.print(sp[i].charAt(sp[i].length()-1));
}


String s = "Bus 02.Port1: Dev1, Class=root_hub, Driver=ehci_hcd/2p, 480M|__ Port1: Dev2, If 0, Class=hub, Driver=hub/6p, 480M|__ Port1: Dev3, If 0, Class";

  int index = -1;

  while ((index = s.indexOf(":", index + 1)) != -1) {

   //indexOf(String str, int fromIndex)方法,从第index个索引处开始查找字符串中冒号":"的位置,找不到时返回-1

   System.err.print(s.charAt(index - 1));//找到冒号的位置index,减去一,即冒号前的一个符号了

  }


扩展阅读:java入门 ... java workbook ... java字符串提供的方法 ... java replace ... java poi ... java 输入输出 ... java 输出26字母组合 ... java 怎么输出汉字 ... java 字符串转utf-8 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网