标签: replaceall

Java:str.replaceAll()不起作用

我正在尝试从另一个中清除一个字符串.

before = before.replaceAll(Constants.GENE_START_SEQUENCE, "");
Run Code Online (Sandbox Code Playgroud)

然而,以下断言有时会失败:

assert before.indexOf(Constants.GENE_START_SEQUENCE) == -1 : before;
Run Code Online (Sandbox Code Playgroud)

这就是断言吐出的内容:

IIAOOOCOAAAOCCIOOOACAIAOACICOOIAIOOICIIOIIOICOICCCOOAOICOCOOIIOOAOAACIIOCCICIOIII
Run Code Online (Sandbox Code Playgroud)

java string replaceall

1
推荐指数
1
解决办法
3624
查看次数

replaceAll和行首(java)

我想使用String的replaceAll函数来删除任何不在给定集合中的char.我试过了

x = x.replaceAll("^[A-Za-z]", "");
Run Code Online (Sandbox Code Playgroud)

但是这不起作用,因为当放置在表达式的开头时,^字符具有另一种含义(匹配行的开头).

我怎样才能得到我想要的行为?我在正确的语法上找不到任何帮助.

谢谢

java regex replaceall

1
推荐指数
1
解决办法
1846
查看次数

Java String.replaceAll用于清理电话号码的方法

我有一个名为TelephoneName的databasefield.在这个领域,我得到了不同格式的电话号码.

我现在需要的是将它们分成国家代码和用户号码.

例如,我看到了电话号码+49(0)711/61947-xx.

我想删除所有斜杠,括号,减号,空格.结果可能是+49(国家代码)和071161947**(subsribernumber).

我怎么能用replaceAll方法做到这一点?

replaceAll("//()-","") 那是对的吗?

事情是我有很多未格式化的电话号码,如:+49 04261 85120 +32027400050使用相同的算法应用每个电话号码是不同的

java string replaceall

1
推荐指数
1
解决办法
6294
查看次数

ArrayList replaceAll()不起作用

我不知道我的代码中存在什么问题.无论我尝试更换什么,而不是畏缩.我有一个private static ArrayList<String> lista.

我填这个.然后在另一种方法中,无论我尝试什么,我都无法替换任何东西,比如:

public static void replacing() {
    Collections.replaceAll(lista, "a", "!!!!!!!!!!!!!!!!!!!!!");
}
Run Code Online (Sandbox Code Playgroud)

然后我在方法中打印这个并且lista是相同的,没有任何改变.我该怎么办?

public class MyProgram {
    private static ArrayList < String > lista;
    public static void fileReading() {
        lista = new ArrayList < String > ();
        try {
            inp = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(inFileNev), "ISO8859-1")));
            String sor;
            while ((sor = inp.readLine()) != null) {
                lista.add(sor);
                lista.add(System.getProperty("line.separator"));
            }
            inp.close();
        } catch...
    }
    public static void searching() {
        Collections.replaceAll(lista, "a", "b");
        System.out.println(lista.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

java arraylist replaceall

1
推荐指数
1
解决办法
2万
查看次数

删除标点符号,保留字母和空格 - Java Regex

今晚我试图从文件中解析单词,我想删除所有标点符号,同时保留大小写单词以及空格。

String alpha = word.replaceAll("[^a-zA-Z]", "");
Run Code Online (Sandbox Code Playgroud)

这将替换所有内容,包括空格。

对包含 的文本文件进行操作Testing, testing, 1, one, 2, two, 3, three.,输出变为TESTINGTESTINGONETWOTHREE 但是,当我将其更改为

String alpha = word.replaceAll("[^a-zA-Z\\s]", "");
Run Code Online (Sandbox Code Playgroud)

输出不会改变。

这是完整的代码片段:

public class UpperCaseScanner {

    public static void main(String[] args) throws FileNotFoundException {

        //First, define the filepath the program will look for. 
        String filename = "file.txt";   //Filename
        String targetFile = "";         
        String workingDir = System.getProperty("user.dir");

        targetFile = workingDir + File.separator + filename;   //Full filepath.

        //System.out.println(targetFile); //Debug code, prints the filepath. 

        Scanner fileScan = new …
Run Code Online (Sandbox Code Playgroud)

java regex string replaceall

1
推荐指数
1
解决办法
2万
查看次数

删除标点符号在Java中不能使用字符串替换

所以,我要做的是编译一个单词列表,没有8个单独的字典单词列表中的重复.一些字典中有标点符号来分隔单词.以下是我所涉及的标点符号删除.我已经尝试了几种不同的解决方案,我发现堆栈溢出有关正则表达式,以及我在代码中留下的那个.出于某种原因,他们都没有从源词典中删除标点符号.有人可以告诉我它是什么我在这里做错了,可能还有如何修复它?我很茫然,有一个同事检查它,他说这应该也可以.

int i = 1;
boolean checker = true;
Scanner inputWords;
PrintWriter writer = new PrintWriter(
        "/home/htarbox/Desktop/fullDictionary.txt");
String comparison, punctReplacer;

ArrayList<String> compilation = new ArrayList<String>();



while (i <9)
{
inputWords = new Scanner(new File("/home/htarbox/Desktop/"+i+".txt"));
    while(inputWords.hasNext())
    {
        punctReplacer = inputWords.next();
        punctReplacer.replaceAll("[;.:\"()!?\\t\\n]", "");
        punctReplacer.replaceAll(",", "");
        punctReplacer.replaceAll("\u201C", "");
        punctReplacer.replaceAll("\u201D", "");
        punctReplacer.replaceAll("’", "'");

        System.out.println(punctReplacer);
        compilation.add(punctReplacer);
    }
    }
inputWords.close();
}
i = 0;
Run Code Online (Sandbox Code Playgroud)

java string arraylist replaceall

1
推荐指数
1
解决办法
525
查看次数

有没有办法在string上使用replaceAll但是调用方法来替换每次匹配时的文本

我想用不同的UUID替换所有出现的特定字符串.例如,

content = content.replaceAll("xyz", "xyz" + generateUUID());
Run Code Online (Sandbox Code Playgroud)

但问题是所有"xyz"的将被相同的UUID取代.但我希望每个"xyz"都被个人唯一ID取代.如何才能做到这一点?

java string replaceall

1
推荐指数
1
解决办法
548
查看次数

java字符串操作,将多个斜杠更改为一个斜杠

愚蠢的错误+用来代替+

所有,我试图将输入路径中的所有"/ +"转换为"/"以简化unix风格的路径.

 path.replaceAll( "/+", "/"); 
 path.replaceAll( "\\/+", "/"); 
Run Code Online (Sandbox Code Playgroud)

事实证明没有做任何事情,这样做的正确方法是什么?

public class SimplifyPath {
public String simplifyPath(String path) {
    Stack<String> direc = new Stack<String> ();
    path = path.replaceAll("/?", "/");
    System.out.println("now path becomes " + path);  // here path remains "///"

    int i = 0;
    while (i < path.length() - 1) {
        int slash = path.indexOf("/", i + 1);
        if (slash == -1) break;
        String tmp = path.substring(i, slash);
        if (tmp.equals("/.")){
            continue;
        } else if (tmp.equals("/..")) {
            if (! direc.empty()){
                direc.pop(); …
Run Code Online (Sandbox Code Playgroud)

java string replaceall

1
推荐指数
1
解决办法
1225
查看次数

为什么replaceAll("$","")不工作虽然replace("$","")工作得很好?

import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
 {
       int turns;
     Scanner scan=new Scanner(System.in);
     turns=scan.nextInt();
     while(turns-->0)
     {
       String pattern=scan.next();
       String text=scan.next();
       System.out.println(regex(pattern,text));

      }
 }//end of main method
 static int regex(String pattern,String text)
 {
     if(pattern.startsWith("^"))
       {
           if(text.startsWith(pattern.replace("^","")))
           return 1;
       }
       else if(pattern.endsWith("$"))
       {
           if(text.endsWith(pattern.replace("$","")))
           return 1;
       }
       else
       {
          if(text.contains(pattern))
          return 1; 
       }
       return 0;
 }
}
Run Code Online (Sandbox Code Playgroud)

输入:2或$ hodor或$ arya

输出:1 0

在这个程序中,我正在扫描两个参数(String),其中第一个是模式,第二个是我必须找到模式的文本.如果模式匹配,方法应该返回1,否则返回0.虽然使用替换它工作正常但是当我将replace()替换为replaceAll()时它没有按预期正常工作.如何使replaceAll()在此程序中工作.

java string replace replaceall

1
推荐指数
1
解决办法
633
查看次数

Java:String.replaceAll(regex,replacement);

我有一串逗号分隔的用户ID,我想从字符串中删除/删除特定的用户ID.

我遵循字符串和预期结果的可能性

int elimiateUserId = 11;

String css1 = "11,22,33,44,55";
String css2 = "22,33,11,44,55";
String css3 = "22,33,44,55,11";
// The expected result in all cases, after replacement, should be:
// "22,33,44,55"
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法:

String result = css#.replaceAll("," + elimiateUserId, "");  // # =  1 or 2 or 3
result = css#.replaceAll(elimiateUserId + "," , "");
Run Code Online (Sandbox Code Playgroud)

这种逻辑在以下情况下失败了css3.请为我提出适当的解决方案.

注意:我正在使用Java 7

我检查了以下帖子,但找不到任何解决方案:

java regex string replace replaceall

1
推荐指数
3
解决办法
3966
查看次数

标签 统计

java ×10

replaceall ×10

string ×8

regex ×3

arraylist ×2

replace ×2