帮我编写个java程序 字符串检索输出并查找 java中输出一个字符串,然后检索字符串中元素所在的位置,会...
JAVA\u7f16\u5199\u4e00\u6bb5\u7a0b\u5e8f\uff0c\u67e5\u627e\u67d0\u4e2a\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u627e\u5230\u5c31\u8f93\u51fa\uff0c\u627e\u4e0d\u5230\u95f4\u9694\u4e00\u6bb5\u65f6\u95f4\u518d\u627e\u601d\u8def\u662f\u8fd9\u6837\uff1a
\u9996\u5148\u83b7\u5f97\u6587\u4ef6\u7684\u540d\u79f0\uff0c\u5339\u914d\u5236\u5b9a\u7684\u5b57\u7b26\uff0c\u5b57\u7b26\u5b58\u5728\uff0c\u7ed3\u675f\u3002\u5426\u5219\uff0c\u7528\u5b9a\u65f6\u5668\uff0c\u8bbe\u7f6e\u65f6\u95f4\u6bb5\uff0c\u5f02\u6b65\u83b7\u5f97\u6587\u4ef6\u540d\u79f0\uff0c\u5339\u914d\u5236\u5b9a\u7684\u5b57\u7b26\uff0c\u5b57\u7b26\u5b58\u5728\uff0c\u7ed3\u675f\u3002\u5426\u5219\u91cd\u590d\u540c\u6837\u7684\u52a8\u4f5c
public class a {
public static void main(String[] args) {
String mail="[email protected]";
System.out.println(name.indexOf("@"));
}
}
index()\u65b9\u6cd5\u5c31\u662f\u8fd4\u56de\u5143\u7d20\u6240\u5728\u4f4d\u7f6e\u7684\u7d22\u5f15\uff0c\u5728javascript\u53ef\u7528\u4e8e\u68c0\u7d22\u90ae\u7bb1@\u7b26\u53f7\u7684\u4f4d\u7f6e\u6765\u5224\u65ad\u90ae\u7bb1\u8f93\u5165\u662f\u5426\u6b63\u786e\u3002
public class Test {
public static void main(String[] args) {
String str = "Eng is England,It is End.";
String key1 = "En";
String key2 = "My";
// 1)统计文章中单词以En开头的单词数量并输出这些单词。
final String reg = "[^a-zA-Z]";
final String flag = "&";
str = str.replaceAll(reg, flag);//将非字母都置换成flag标记,用以区别单词
String[] array = str.split(flag);// 以flag标记区别单词
int count = 0;
for (String s : array) {
if (s.contains(key1)) {
System.out.println(s);
++count;
}
}
System.out.println("数量" + count);
// 2)查找字符串“My”,输出位置。
if (str.contains(key2)) {
// 如果不需要区分大小写还可以这么写: str.toLowerCase().contains(key2.toLowerCase())
System.out.println("下标为:" + str.indexOf(key2));
} else {
System.out.println("不存在..");
}
}
}
public static int getSubCount_2(String str,String key) { int count = 0; int index = 0; while ((index=str.indexOf(key,index))!=-1) { sop("index="+index); index = index + key.length(); count++; } return count; } public static void main(String[] args) { String str = "Eng is England,It is End"; String key = "My"; sop("count="+getSubCount_2(str,key)); }
public static void sop(String str) { System.out.println(str); }
细节方面你自己微调一下。
package string;
import java.util.regex.Matcher;import java.util.regex.Pattern;
public class SearchStr {
public static void search(String str0,String str1,String str2){ Pattern pattern=Pattern.compile(str1); Matcher match=pattern.matcher(str0); int count=0; while(match.find()){ count++; System.out.println(match.group()); } System.out.println("count="+count); match=Pattern.compile(str2).matcher(str0); while(match.find()){ System.out.println(str2+"起始索引位置:"+match.start()); return; } System.out.println("没有发现单词"+str2); } public static void main(String[] args){ String str0="Eng is England,It is End."; String str1="(En)\\w+"; String str2="(My)"; search(str0,str1,str2); }}
import java.io.*;public class Indexx { public static void main(String[] args) throws IOException { BufferedReader Br = new BufferedReader(new FileReader("这里讲英文文章内容复制到一个新建文档里面,然后把该文档的绝对路劲复制进来.")); StringBuffer s = new StringBuffer(); //用来保存文档的内容 String data = new String(); int sign = 1; //记录My单词出现的位置 while( (data = Br.readLine()) != null){ s.append(data); } //将文档的内容全部写入s中 for(int i = 0; i < s.length(); i++){ //遍历文件,开始检索 if(s.charAt(i) == 'E' && s.charAt(i+1) == 'n'){ String subs = new String(""); for(int j = i; j < s.length(); j++){ if((s.charAt(j) >= 'a' && s.charAt(j) <= 'z') || (s.charAt(j) >= 'A' && s.charAt(j) <= 'Z')){ subs += s.charAt(j); }else{ System.out.println(subs); break; } } } if(s.charAt(i) == 'M' && s.charAt(i+1) == 'y' && s.charAt(i+2) ==' '){ System.out.println("单词My第" + sign +"次出现的位置为: " + (i+1)); sign++; } } }
}
差不多就这样吧, 我初学JAVA,不足还望见谅~
我了个去怎么每次贴代码都这么乱啊.. LZ你直接复制进编译软件里面然后整理运行吧.
import java.util.ArrayList;
public class Demo {
private ArrayList<String> words = new ArrayList<String>();
/**
*
* @param str 要统计的字符串
* @param pre 要统计的单词的前缀
* @param find 要查找子串的首次出现位置
*/
public void count(String str, String pre, String find) {
String ex = "[\\pP]";
// 查找字符串My的位置
if (str.indexOf(find) != -1) {
int a = str.indexOf(find);
System.out.println("字符串【" + find + "】的位置:" + a);
} else {
System.out.println("字符串【" + find + "】的位置不存在");
}
// 统计En出现次数
// 将标点符号替换成空格
str = str.replaceAll(ex, " ");
// 按空格分隔
String[] temp = str.split(" ");
// 统计En出现
for (int i = 0; i < temp.length; i++) {
if (temp[i].startsWith(pre)) {
words.add(temp[i]);
}
}
System.out.println("以En开头的单词数量为:" + words.size() + ".");
System.out.println("单词依次为:");
for (int i = 0; i < words.size(); i++) {
System.out.println("第" + (i + 1) + "个:" + words.get(i));
}
}
public static void main(String[] args) {
String str = "Eng is England,It is End.";
Demo d = new Demo();
d.count(str, "En", "My");
}
}
----------------
仅供参考
扩展阅读:java程序设计 陈锐 ... java replace替换字符 ... java程序设计 苏俊 ... java数组 ... java程序设计 吴萍 ... java 转义字符 ... java中字符 ... 用java输出依次递增的星号 ... java从键盘输入一个字符 ...