当前位置: 代码迷 >> Web前端 >> java中怎么反向显示字符串(包含实际开发中的例子)(转载)
  详细解决方案

java中怎么反向显示字符串(包含实际开发中的例子)(转载)

热度:160   发布时间:2012-10-23 12:12:22.0
java中怎样反向显示字符串(包含实际开发中的例子)(转载)

法1.借助StringBuilder
String str="abcd123";
StringBuilder sb=new StringBuilder(str);
str=sb.reverse().toString;

法2.利用循环
String str="abcd123";
char[] chs=str.toCharArray();
int size=str.length();
char temp;
for(int i=0;i<size/2;i++){
temp=chs[i];
chs[i]=chs[size-1-i];
chs[size-1-i]=temp;
}
str=new String(chs);//翻转后的字符串

下面是实际开发时使用例子:

在extjs提供的HtmlEditor的基础上进行优化,原有的HtmlEditor不支持图片上传,修改后的HtmlEditor代码如下:

HTMLEditor = Ext.extend(Ext.form.HtmlEditor, {
??? addImage : function() {
??????? var editor = this;
??????? var imgform = new Ext.FormPanel({
??????????? region : 'center',
??????????? labelWidth : 55,
??????????? frame : true,
??????????? bodyStyle : 'padding:5px 5px 0',
??????????? autoScroll : true,
??????????? border : false,
??????????? fileUpload : true,
??????????? items : [
??????????????? {
??????????????????? xtype : 'textfield',
??????????????????? fieldLabel : '选择文件',
??????????????????? name : 'userfile',
??????????????????? inputType : 'file',
??????????????????? allowBlank : false,
??????????????????? blankText : '文件不能为空',
??????????????????? height : 25,
??????????????????? anchor : '90%'
??????????????? }
??????????? ],
??????????? buttons : [
??????????????? {
??????????????????? text : '上传',
??????????????????? type : 'submit',
??????????????????? handler : function() {
??????????????????????? if (!imgform.form.isValid()) {
??????????????????????????? return;
??????????????????????? }
??????????????????????? imgform.form.submit({
??????????????????????????? waitMsg : '正在上传',
??????????????????????????? url : 'EditImg.jsp',
??????????????????????????? success : function(form, action) {
??????????????????????????????? var element = document.createElement("img");
????????????????????????????????var responseArray = Ext.util.JSON.decode(action.response.responseText);
????????????????????????????????if (responseArray.success) {
??????????????????????????????????? var qty = responseArray.fileURL;???//注意此处是如何从后台jsp页面获取参数的(同时jsp页面是如何向js页面传参数的,后面的jsp页面代码有标注)

??????????????????????????????????? element.src = qty;
??????????????????????????????????? editor.insertAtCursor(element.outerHTML);
??????????????????????????????????? win.hide();
??????????????????????????????? }
??????????????????????????? }
??????????????????????????????????????????????????? });
??????????????????? }
??????????????? },
??????????????? {
??????????????????? text : '关闭',
??????????????????? type : 'submit',
??????????????????? handler : function() {
??????????????????????? win.close(this);
??????????????????? }
??????????????? }
??????????? ]
??????? })

??????? var win = new Ext.Window({
??????????? title : "上传图片",
??????????? width : 300,
??????????? height : 200,
??????????? modal : true,
??????????? border : false,
??????????? iconCls : "img.gif",
??????????? layout : "fit",
??????????? items : imgform

??????? });
??????? win.show();
??? },
??? createToolbar : function(editor) {
??????? HTMLEditor.superclass.createToolbar.call(this, editor);
??????? this.tb.insertButton(16, {
??????????? cls : "x-btn-icon",
??????????? icon : "img.gif",
??????????? handler : this.addImage,
??????????? scope : this
??????? });
??? }
});
Ext.reg('StarHtmleditor', HTMLEditor);

有了以上这些,然后调用StarHtmleditor即可在原有的htmleditor组件中显示一个图片按钮控件,如果仅做这些还不够,还要看图片的存储位置,在以下jsp代码中调用了好多特殊方法,值得借鉴:

<%@ page import="static com.wlcsp.project.core.AppService.getappService" %>
<%@ page import="com.wlcsp.project.core.AppRequest" %>
<%@ page import="com.wlcsp.project.core.AppTemplate" %>
<%@ page import="com.wlcsp.project.bean.frameapp.kms.KmsDocument" %>
<%@ page import="com.wlcsp.project.bean.frameapp.kms.KmsDocumentAttachment" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="com.wlcsp.project.biz.SMBManager" %>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
??? final AppTemplate template = new AppTemplate(request, response, config, pageContext);
??? final AppRequest req = new AppRequest(request);
%>
<%
??? mySmartUpload.init(config);
??? mySmartUpload.service(request, response);
??? mySmartUpload.upload();
??? String filepath = "";
??? String filename = "";
??? String fileurl = "";
??? String name = "";
??? String systemname = "";
??? for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
??????? com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
??????? if (!myFile.isMissing()) {
??????????? filename = myFile.getFileName();
??????????? name = myFile.getFileName();
??????????? int filesize = myFile.getSize();
??????????? Timestamp timestamp = new Timestamp(System.currentTimeMillis());
??????????? java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");
??????????? String creattime = sdf.format(timestamp);
??????????? systemname = creattime + name;
??????????? myFile.saveAs("/docfile/kms/" + systemname);
??????????? String s =?template.getRealPath("/");
????????????StringBuilder sb = new StringBuilder(s);
??????????? s = sb.reverse().toString();
??????????? s = s.substring(2, s.length());
??????????? sb = new StringBuilder(s);
??????????? s = sb.reverse().toString();
??????????? String path =?SMBManager.getConfig().getKmsfile();
??????????? String templateFileName = s + path;
??????????? templateFileName = templateFileName + systemname;
??????????? templateFileName =?templateFileName.replace("\\", "/");
??????????? fileurl = "E:/svn/new/Project/web/docfile/kms/" + systemname;
????????????out.print("{success:true,fileURL:'" + templateFileName + "'}");
??????? }
??? }

%>

replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:?

1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也就是字符串);?
2)replaceAll的参数是regex,即基于规则表达式的替换,比如,可以通过replaceAll("\\d", "*")把一个字符串所有的数字字符都换成星号;?

??? 相同点是都是全部替换,即把源字符串中的某一字符或字符串全部换成指定的字符或字符串,如果只想替换第一次出现的,可以使用 replaceFirst(),这个方法也是基于规则表达式的替换,但与replaceAll()不同的是,只替换第一次出现的字符串;?
??? 另外,如果replaceAll()和replaceFirst()所用的参数据不是基于规则表达式的,则与replace()替换字符串的效果是一样的,即这两者也支持字符串的操作;?
??? 还有一点注意:执行了替换操作后,源字符串的内容是没有发生改变的.?

举例如下:?
????????
??????? String src = new String("ab43a2c43d");?

??????? System.out.println(src.replace("3","f"));=>ab4f2c4fd.?
??????? System.out.println(src.replace('3','f'));=>ab4f2c4fd.?
??????? System.out.println(src.replaceAll("\\d","f"));=>abffafcffd.?
??????? System.out.println(src.replaceAll("a","f"));=>fb43fc23d.?
??????? System.out.println(src.replaceFirst("\\d,"f"));=>abf32c43d?
??????? System.out.println(src.replaceFirst("4","h"));=>abh32c43d.?


如何将字符串中的"\"替换成"\\":?

??????? String msgIn;?
??????? String msgOut;?
??????? msgOut=msgIn.replaceAll("\\\\","\\\\\\\\");?
原因:?
???????? '\'在java中是一个转义字符,所以需要用两个代表一个。例如System.out.println( "\\" ) ;只打印出一个"\"。但是'\'也是正则表达式中的转义字符(replaceAll 的参数就是正则表达式),需要用两个代表一个。所以:\\\\被java转换成\\,\\又被正则表达式转换成\。?
同样?
??????? CODE: \\\\\\\\?
??????? Java: \\\\?
??????? Regex: \\?


将字符串中的'/'替换成'\'的几种方式:?

msgOut= msgIn.replaceAll("/", "\\\\");?
???????? msgOut= msgIn.replace("/", "\\");?
???????? msgOut= msgIn.replace('/', '\\');

  相关解决方案