String.replace和String.replaceAll不适用于空白的方法

时间:2017-05-03 19:45:32

标签: java

我有一个字符串,我需要用“”或“_”替换所有空格和“和”。但是我的代码不起作用。

    text = text.replaceAll("\\x0B","");

产生的输出是“工作_旅行”

我找到了解决方案。谢谢您的帮助。有一个我看不到的非休息空间。所以工作方法是

<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="IndexStyle.css"/>
    <meta name="viewport" content="width= device-width, intial-scale=1.0">
</head>
<body class="body">
    <header class="MainHeader">
    <img src="images/header-image-5.jpg">
    <nav><ul>
         <li class="active"><a href="index.php">Home</a></li>
         <li><a href="Product.php">Products</a></li>
         <li><a href="AboutUs.php">About Us</a></li>
         <li><a href="ContactUs.php">Contact Us</a></li>
        </ul>
    </nav>
    </header>
    <div class="MainBody">
    <h1>Products
      </h1>
      <div class="productsresult">
<?php if ($totalRows_ProductsData > 0) { // Show if recordset not empty ?>
<?php do { ?>
    <div class="products"> <p><?php echo $row_ProductsData['product_name']; ?>
        <?php echo $row_ProductsData['price']; ?>
        <?php echo $row_ProductsData['image']; ?></p>
        <?php } while ($row_ProductsData = mysql_fetch_assoc($ProductsData)); ?>
    </div>
<?php } // Show if recordset not empty ?>
<?php if ($totalRows_ProductsData == 0) { // Show if recordset empty ?>
  There are no products in this category!
  <?php } // Show if recordset empty ?>
     </div>
     </div>
    <aside class="SideBar">
<?php include ("/includes/catalogue.php"); ?>
    </aside>
    <Footer class="Footer"> 
        <p>Copyrights reserved 
        </p>

    </Footer>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

public class test {
    public static void main(String [] args) {
        String text = "working and travelling";
        text = text.replaceAll("and","_").replaceAll(" ","");

        System.out.println(text);
    }
}

输出是“working_travelling”