Java 不使用(子字符串)函数从字符串中删除特定字符
出于学习目的,我需要从字符串中提取“del”,而不使用(substring)函数 *请注意,字符串不是固定的,可以更改 我在下面的代码中解决了它,但是我需要一些帮助来获得更好的代码,因为我的代码有很多行,我需要一个更好的解决方案Java 不使用(子字符串)函数从字符串中删除特定字符,java,Java,出于学习目的,我需要从字符串中提取“del”,而不使用(substring)函数 *请注意,字符串不是固定的,可以更改 我在下面的代码中解决了它,但是我需要一些帮助来获得更好的代码,因为我的代码有很多行,我需要一个更好的解决方案 public static String delDel(String str) { int num = 0; boolean s = false; char[] a = new char[str.length()]; char[] b =
public static String delDel(String str) {
int num = 0;
boolean s = false;
char[] a = new char[str.length()];
char[] b = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) == 'd')) {
for (int j = 0; j < 1; j++) {
a[j] = 'd';
}
num = i;
s = true;
break;
}
}
if (s && (str.charAt(num + 1) == 'e') && (str.charAt(num + 2) == 'l')) {
a[num] = str.charAt(num + 1);
a[num + 1] = str.charAt(num + 2);
}
String t = new String(a).trim();
System.out.println(t);
if (t.equalsIgnoreCase("del")) {
for (int i = 0; i < num; i++) {
b[i] = str.charAt(i);
}
for(int j =1; j<str.length()-3;j++)
{
for (int ii = num + 3; ii < str.length(); ii++) {
b[j] = str.charAt(ii);
j++;
}}
String tt = new String(b).trim();
return tt;
}
return str;
}
public static void main(String[] args) {
System.out.println(delDel("adelrtyc"));
}
publicstaticstringdeldeldel(stringstr){
int num=0;
布尔s=假;
char[]a=新字符[str.length()];
char[]b=新字符[str.length()];
对于(int i=0;i
String text = "adelrtyc";
text = text.replaceAll("del","");
在巴尔东有一篇关于番石榴的好文章
太棒了
哦!我明白了:它是用来教育的!不,番石榴是用来的:-)有很多种方法
String del=“del”;
String str=“thisdelfoo”;
//方法1(只需从生成器中删除字符
StringBuilder sb=新的StringBuilder(str);
sb.删除(idx,idx+del.length());
字符串newStr=sb.toString();
System.out.println(newStr);
//方法2(请注意str.replace将替换所有方法)。
newStr=str.replaceFirst(del,“”);
System.out.println(newStr);
听起来您真正的问题是如何清理现有代码以使其更具可读性,而不是哪一个库可以在一行中为您解决问题
我想读一读马丁·福勒的书
您将了解到,像int num
和boolean s
这样的变量名不可能一眼就能理解
听起来你还想知道你的算法是否最优。实际上它有问题,所以让我们从这里开始。它对像“ddel”
这样的字符串不起作用,因为你只检查找到的第一个'd'
您的算法:
德尔德尔(str)
查找str
中包含的第一个'd'
的索引num
如果后面跟着一个“el”
,请继续
重建str
,除了num
、num+1
和num+2
处的字符
第1步和第2步是导致错误的原因
修改算法:
德尔德尔(str)
查找str
中包含的下一个'd'
的索引num
如果后面跟着一个“el”
,则继续,否则重复步骤1
重建str
,除了num
、num+1
和num+2
处的字符
这里的一些操作是冗余的,这就是为什么我假设您提到您的代码有“很多行”的原因。下面是一些减少这种情况的技巧:
您可以在检查'd'
的同一循环中检查'e'
和'l'
String
有一个方法tocharray()
,您应该对此进行调查
创建字符串t
并检查其是否等于“del”
的逻辑可以替换为简单的布尔标志
好的,这里有一个没有删除方法的解决方案
public class StringDel {
private static char[] DEL_CHARS = "del".toCharArray();
public static String delDel(String string) {
char[] strChars = string.toCharArray();
// strIdx moves through the input string
int strIdx = 0;
// delIdx moves through the "del" string
// as long as it maches the input string
int delIdx = 0;
// Will build the new string
StringBuilder sb = new StringBuilder();
// Start from the begining
while( strIdx < strChars.length ) {
// Does the char at the acutal position mach
// the char in the delStr Position?
if( strChars[strIdx]==DEL_CHARS[delIdx]) {
// YES! Move on in the delStr
delIdx++;
strIdx++;
} else {
// NO! OK, append the String to the output
// Special condition! We could have read a part of DEL_CHARS
if( delIdx>0 ) {
// Copy that part to the target string
for( int i=0; i<delIdx; i++) {
sb.append(DEL_CHARS[i]);
}
}
delIdx=0;
// AND ANOTHER SPECIAL Specialtrick
// Maybe the current char is like the
// first char in CHAR_DEL. Then we have
// a fist match and need to handle it
// the same way.
if( strChars[strIdx]==DEL_CHARS[delIdx] ) {
delIdx++;
} else {
// Otherwise we can copy the char
// to the target string
sb.append(strChars[strIdx]);
}
strIdx++;
}
if( delIdx==DEL_CHARS.length ) {
delIdx = 0;
}
}
return sb.toString();
}
public static void main( String[] args ) {
System.out.println(delDel("deRmovededeldelFromThisdel"));
}
}
公共类StringDel{
私有静态字符[]DEL_CHARS=“DEL”.toCharArray();
公共静态字符串delDel(字符串){
char[]strChars=string.tocharray();
//strIdx在输入字符串中移动
int-strIdx=0;
//delIdx在“del”字符串中移动
//只要它处理输入字符串
int-delIdx=0;
//将生成新字符串
StringBuilder sb=新的StringBuilder();
//从头开始
while(strIdx0){
//将该部分复制到目标字符串
对于(int i=0;i有很多行并不一定意味着代码不好,但是,在某些地方,您的格式是错误的,不一致的,在我看来,这比一些糟糕的代码逻辑更糟糕。这是一些令人困惑的代码。对于(int j=0;j<1;j++){a[j]='d';}
(第8-10行)与a[0]不一样吗='d';
?@Nexevis是的,我需要提高这方面的技能,非常感谢man@f1sh是的,这是相同的,需要编辑,非常感谢。不需要用包装del
(…)+
因为replaceAll
替换了指定目标的每次出现,也替换了连续的目标。@Pshemo我这样做是为了强调正则表达式的使用,但是我已经对它进行了编辑。谢谢,因为问题特别指出不要使用子字符串函数,我只能想象他们想要在较低的级别上解决它,而不仅仅是使用不同的内置函数。在这里使用regexreplaceAll
而不是text.replace(“del”),有什么意义呢?
,因为你根本没有使用regex的功能。(我想了解更多关于其优点的解释。)