如何在Java中动态替换字符串?

时间:2019-03-12 09:56:30

标签: java

我正在尝试使用String.replace(oldChar,newChar),但这将返回更改后的字符串。  示例:

String tempString= "abc is very easy";
String replacedString=tempString.replace("very","not");
System.out.println("replacedString is "+replacedString);
System.out.println("tempString is "+tempString);

输出:

replacedString is abc is not easy
tempString is abc is very easy

所以我的问题是有什么方法可以替换字符串而不将其存储在另一个字符串中。

我要输出

String tempString= "abc is very easy";
tempString.replace("very","not");
System.out.println("tempString is "+tempString);

tempString is abc is not easy.

通过使用替换功能。

2 个答案:

答案 0 :(得分:5)

字符串是不可变的。他们无法改变。您对它们执行的任何操作都将导致您调用的方法返回一个“新”字符串。

了解更多信息:Immutability of Strings in Java

因此,在您的示例中,如果要更改字符串,则需要覆盖引用原始字符串的变量,以指向新的字符串值。

String tempString= "abc is very easy";
tempString = tempString.replace("very","not");
System.out.println("tempString is "+tempString);

为您的理解提供内幕:

  

将字符串“ abc非常简单”分配给内存地址0x03333
  将地址0x03333分配给变量tempString
  调用方法replace,在地址0x05555处创建新的修改后的字符串
  将地址0x05555分配给临时字符串变量
  从方法replace返回临时字符串变量
  将地址从temp String变量分配给变量tempString
  tempString中的地址现在为0x05555。
  如果代码定义的字符串和StringPool中没有0x03333的字符串,则地址0x03333的字符串将被垃圾回收。

答案 1 :(得分:-1)

您可以用新值<!DOCTYPE html> <html lang="en"> <head> <?php // Create connection include('../connection.php'); // Request $requete = $pdo->prepare( ' SELECT * FROM setting S;' ); $requete->execute(); $datas = $requete->fetchAll(); ?> <!-- Bootstrap core CSS --> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"> <!-- FooTable Bootstrap CSS --> <link href="https://fooplugins.github.io/FooTable/compiled/footable.bootstrap.min.css" rel="stylesheet"> <!-- Custom styles --> <link href="https://fooplugins.github.io/FooTable/docs/css/docs.css" rel="stylesheet"> <script src="https://fooplugins.github.io/FooTable/docs/js/demo-rows.js"></script> </head> <body class="docs"> <!-- Header --> <!-- Content --> <div class="container"> <div class="docs-section"> <div class="example"> <table id="editing-example" class="table" data-paging="true" data-filtering="false" data-sorting="false"> <thead> <tr> <th data-name="id" data-breakpoints="xs" data-type="number">ID</th> <th data-name="nom_config">Name</th> <th data-name="img_logo_content">Logo</th> </tr> </thead> <tbody> <?php foreach( $datas as $data ) { ?> <tr data-expanded="true"> <td> <?php echo $data->id; ?></td> <td> <?php echo $data->nom_config; ?></td> <td> <?php echo '<img height="20" src="data:image;base64,' . $data->img_logo_content . '">' ?></td> </tr> <?php } ?> </tbody> </table> <!-- Modal --> <div class="modal fade" id="editor-modal" tabindex="-1" role="dialog" aria-labelledby="editor-title"> <style scoped> .form-group.required .control-label:after { content: "*"; color: red; margin-left: 4px; } </style> <div class="modal-dialog" role="document"> <form class="modal-content form-horizontal" id="editor"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span> </button> <h4 class="modal-title" id="editor-title">Add Row</h4> </div> <div class="modal-body"> <input type="number" id="id" name="id" class="hidden" /> <div class="form-group required"> <label for="nom_config" class="col-sm-4 control-label">Name</label> <div class="col-sm-8"> <input type="text" class="form-control" id="nom_config" name="nom_config" required> </div> </div> <?php //************************************************************* // PROBLEME HERE !!! Find the logo picture from sql database $sql = "select * from setting WHERE id='" . $id . "'"; $result = mysqli_query($conn, $sql); while ($row = mysqli_fetch_array($result)) { $img_logo= '<img height="50" src="data:image;base64,' . $row[22] . '">'; } //************************************************************* ?> <div class="form-group"> <label for="img_logo_content" class="col-sm-4 control-label">logo</label> <div class="col-sm-8"> <?php // Display Logo echo $img_logo; ?> <input type="file" class="form-control" id="img_logo_content" name="img_logo_content" placeholder="Votre Logo"> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary">Save changes</button> <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> </div> </form> </div> </div> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="https://fooplugins.github.io/FooTable/docs/js/prism.js"></script> <script src="https://fooplugins.github.io/FooTable/docs/js/ie10-viewport-bug-workaround.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script> <script src="https://fooplugins.github.io/FooTable/compiled/footable.js"></script> <!-- Initialize FooTable --> <script> jQuery(function($) { var $modal = $('#editor-modal'), $editor = $('#editor'), $editorTitle = $('#editor-title'), ft = FooTable.init('#editing-example', { editing: { enabled: true, addRow: function() { $modal.removeData('row'); $editor[0].reset(); $editorTitle.text('Add a new row'); $modal.modal('show'); }, editRow: function(row) { var values = row.val(); $editor.find('#id').val(values.id); $editor.find('#nom_config').val(values.nom_config); $editor.find('#img_logo_name').val(values.img_logo_name); $modal.data('row', row); $editorTitle.text('Edit row #' + values.id); $modal.modal('show'); }, deleteRow: function(row) { if (confirm('Are you sure you want to delete the row?')) { row.delete(); } } } }), uid = 10; $editor.on('submit', function(e) { if (this.checkValidity && !this.checkValidity()) return; e.preventDefault(); var row = $modal.data('row'), values = { id: $editor.find('#id').val(), nom_config: $editor.find('#nom_config').val(), img_logo_name: $editor.find('#img_logo_name').val() }; if (row instanceof FooTable.Row) { $.ajax({ url: 'update.php', dataType: "json", cache: false, data: { id: values['id'], nom_config: values['nom_config'], img_logo_content: values['img_logo_content'] }, success: function(data) { if (data.return) { alert("Success"); row.val(values); } else { alert("No modifications!"); } }, }); } else { $.ajax({ url: 'insert.php', data: { id: values['id'], nom_config: values['nom_config'], img_logo_content: values['img_logo_content'] }, success: function(data) { if (data.return) { alert("Success"); ft.rows.add(values); location.reload(); } else { alert("Already used!"); } }, }); } $modal.modal('hide'); }); }); </script> </body> </html> 重新分配:

const defaultAssetExts = require("metro-config/src/defaults/defaults").assetExts;

module.exports = {
  resolver: {
    assetExts: [
      ...defaultAssetExts,
      // 3D Model formats
      "dae",
      "obj",
      "mtl",
      // sqlite format
      "db"
    ]
  }
};

结果是:

tempString

最佳