对于想了解从Java中的另一个字符串中删除字符串的读者,本文将是一篇不可错过的文章,我们将详细介绍从java中的另一个字符串中删除字符串空格,并且为您提供关于ES6--javascript判断一个字符
对于想了解从Java中的另一个字符串中删除字符串的读者,本文将是一篇不可错过的文章,我们将详细介绍从java中的另一个字符串中删除字符串空格,并且为您提供关于ES6--javascript 判断一个字符串是否存在另一个字符串中、java – 如何删除字符串中的一个字符后的所有字符?、Java 实例 - 删除字符串中的一个字符、Java 获取一个字符串中,另一个字符串出现的次数的有价值信息。
本文目录一览:- 从Java中的另一个字符串中删除字符串(从java中的另一个字符串中删除字符串空格)
- ES6--javascript 判断一个字符串是否存在另一个字符串中
- java – 如何删除字符串中的一个字符后的所有字符?
- Java 实例 - 删除字符串中的一个字符
- Java 获取一个字符串中,另一个字符串出现的次数
从Java中的另一个字符串中删除字符串(从java中的另一个字符串中删除字符串空格)
可以说我有这个单词列表:
String[] stopWords = new String[]{"i","a","and","about","an","are","as","at","be","by","com","for","from","how","in","is","it","not","of","on","or","that","the","this","to","was","what","when","where","who","will","with","the","www"};
比我有文字
String text = "I would like to do a nice novel about nature AND people"
是否有匹配stopWords并在忽略大小写时将其删除的方法;像这样的地方?:
String noStopWordsText = remove(text, stopWords);
结果:
" would like do nice novel nature people"
如果您了解正则表达式,效果很好,但我真的更喜欢像Commons解决方案这样的东西,它更注重性能。
顺便说一句,现在我正在使用此通用方法,该方法缺少适当的不区分大小写的处理:
private static final String[] stopWords = new String[]{"i", "a", "and", "about", "an", "are", "as", "at", "be", "by", "com", "for", "from", "how", "in", "is", "it", "not", "of", "on", "or", "that", "the", "this", "to", "was", "what", "when", "where", "who", "will", "with", "the", "www", "I", "A", "AND", "ABOUT", "AN", "ARE", "AS", "AT", "BE", "BY", "COM", "FOR", "FROM", "HOW", "IN", "IS", "IT", "NOT", "OF", "ON", "OR", "THAT", "THE", "THIS", "TO", "WAS", "WHAT", "WHEN", "WHERE", "WHO", "WILL", "WITH", "THE", "WWW"}; private static final String[] blanksForStopWords = new String[]{"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}; noStopWordsText = StringUtils.replaceEach(text, stopWords, blanksForStopWords);
答案1
小编典典这是不使用正则表达式的解决方案。我认为它不如我的其他答案,因为它更长且不清楚,但是如果性能确实非常重要,那么这就是 O(n) ,其中 n
是文本的长度。
Set<String> stopWords = new HashSet<String>();stopWords.add("a");stopWords.add("and");// and so on ...String sampleText = "I would like to do a nice novel about nature AND people";StringBuffer clean = new StringBuffer();int index = 0;while (index < sampleText.length) { // the only word delimiter supported is space, if you want other // delimiters you have to do a series of indexOf calls and see which // one gives the smallest index, or use regex int nextIndex = sampleText.indexOf(" ", index); if (nextIndex == -1) { nextIndex = sampleText.length - 1; } String word = sampleText.substring(index, nextIndex); if (!stopWords.contains(word.toLowerCase())) { clean.append(word); if (nextIndex < sampleText.length) { // this adds the word delimiter, e.g. the following space clean.append(sampleText.substring(nextIndex, nextIndex + 1)); } } index = nextIndex + 1;}System.out.println("Stop words removed: " + clean.toString());
ES6--javascript 判断一个字符串是否存在另一个字符串中
es5 中我们经常使用 indexof () 方法来判断一个字符串是否包含另外一个字符串中。
如果存在则返回匹配到的第一个索引值。如果没有则返回 -1。所以,判断一个字符串是否包含另外一个字符串中只需要判断是否为 - 1 就行。-1 代表不存在。
例如:
let str = ''Hello World!'';
console.log(str.indexOf(''H''));//0 str中"H"的出现的第一个索引为0
console.log(str.indexOf(''o''));//4 str中"o"第一个出现的位置的索引为4
console.log(str.indexOf(''a''));//-1 没找到,返回-1
虽然 Es5 中该方法经常使用,但是 Es6 提供了更加便捷的方法。
1. str.includes('''');
有返回 true,没有返回 false。也不用为记住 - 1 而发愁了!!
let str = ''Hello World!'';
console.log(str.includes(''H''));//true
console.log(str.includes(''a''));//false
2.startsWith()
判断该字符串是否为某个字符串的首位。有就是 true,不是就是 false。
let str = ''Hello World!'';
console.log(str.startsWith(''H''));//true
console.log(str.startsWith(''Hello''));//true
console.log(str.startsWith(''e''));//false
3.endsWith()
和 startsWith () 相反。判断是否为末尾。
let str = ''Hello World!'';
console.log(str.endsWith(''!''));//true
console.log(str.endsWith(''d!''));//true
console.log(str.endsWith(''e''));//false
这三个方法都支持第二个参数,表示看是搜索的位置。
let str = ''Hello World!'';
console.log(str.includes(''World'',5));//true 从索引5(包含索引5)开始搜索
console.log(str.includes(''World'',7));//false
console.log(str.startsWith(''lo'',3))//true
console.log(str.startsWith(''H'',3));//false
console.log(str.endsWith(''el'',3));//true
endsWith()和上面两个不一样,它的第二个参数代表前几个。“Hel”,所以返回true
java – 如何删除字符串中的一个字符后的所有字符?
我想只提取这个字符串的日期,所以最后我会有“01-01-2012”,但不知道如何做到这一点.
解决方法
>将整个事情解析为日期/时间,然后只是取得日期部分(Joda Time或SimpleDateFormat)
>使用indexOf查找第一个空格,并使用substring获取前导子字符串:
int spaceIndex = text.indexOf(" "); if (spaceIndex != -1) { text = text.substring(0,spaceIndex); }
>相信它以指定的格式有效,第一个空格将始终为索引10:
text = text.substring(0,10);
>用空格拆分字符串,然后取第一个结果(对我来说似乎不必要的低效,但它会工作…)
text = text.split(" ")[0];
如果没有空间,你应该考虑你想要发生的事情.这是否意味着数据无效开始?你应该继续整个字符串吗?这将取决于你的情况.
我个人可能会选择第一个选项 – 你真的想解析“01-01-2012 wibble-wobble bad data”,就好像它是一个有效的日期/时间?
Java 实例 - 删除字符串中的一个字符
package string;
public class deleteString {
/**
* 删除字符串
* @param args
*/
public static void main(String[] args) {
String str = "helloo,this is my third blog";
String str1 = removeCharAt(str, 5);
System.out.println(str1);
}
public static String removeCharAt(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 1);// 使用substring()方法截取0-pos之间的字符串+pos之后的字符串,相当于将要把要删除的字符串删除
}
}
输出结果
Java 获取一个字符串中,另一个字符串出现的次数
Java 获取一个字符串中,另一个字符串出现的次数
思想:
1. indexOf到字符串中到第一次出现的索引
2. 找到的索引+被找字符串长度,截取字符串
3. 计数器++
代码实现:
1 public class Test {
2 public static void main(String[] args) {
3 String str="helloword";
4 fun(str,"hello");
5 }
6 public static void fun(String str,String m){
7 //m代表字符的长度
8 int count=0;
9 while(str.indexOf(m)>=0){
10 int index=str.indexOf(m)+m.length();//获取每次找到之后的下标位置
11 str=str.substring(index);
12 count++;
13 }
14 System.out.println("指定字符串在原字符中出現:"+count+"次");
15 }
16 }
总结:最开始就是不明白为什么需要加上字符串的长度
今天的关于从Java中的另一个字符串中删除字符串和从java中的另一个字符串中删除字符串空格的分享已经结束,谢谢您的关注,如果想了解更多关于ES6--javascript 判断一个字符串是否存在另一个字符串中、java – 如何删除字符串中的一个字符后的所有字符?、Java 实例 - 删除字符串中的一个字符、Java 获取一个字符串中,另一个字符串出现的次数的相关知识,请在本站进行查询。
本文标签: