论坛首页 Web前端技术论坛

字符串截取方法总结

浏览 7588 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (6) :: 隐藏帖 (1)
作者 正文
   发表时间:2012-03-11  
字符串截取
今在jsp页面中截取一段字符显示.项目用的struts2标签
经过反复的测试,struts2皆不支持标签的嵌套:
单独支持:
<s:property value='#attr.todo.description.indexOf('2')'/>

<s:property value='#attr.todo.description.length()'/>	

但不支持这种写法:
<s:property value="#attr.todo.description.substring(<s:property value='#attr.todo.description'/>, 
 <s:property value='#attr.todo.description.indexOf('2')'/>,
 <s:property value='#attr.todo.description.length()'/>)">
</s:property>

其<s:textfield>标签格式化显示日期,如下写法:
<s:textfield label="日期" name="todo.created" readonly="true">
      <s:param name="value">
	<s:date name="#attr.todo.created" format="yyyy-MM-dd HH:mm:ss"/>
      </s:param>
</s:textfield>

话说:条条大道通罗马,只能换用EL表达式试试了,结果是一试就通,EL函数很给力啊!!!
${fn:substring(todo.description,fn:indexOf(todo.description,2),fn:length(todo.description))}

但注意的是:单独用需要"$",而嵌套来用嵌套部分的不需要"$",写了反而有问题的.
截取方法的总结:
public class StringSub {
    public static void main(String[] argu)
     {
    String str="abcdefghmf";      
    System.out.println("截取前三个字符:"+str.substring(0, 3)); 
    System.out.println("截取前三个字符以外的字符:="+str.substring(3));  
    System.out.println("截取后三个字符:"+str.substring(str.length()-3,str.length()));  
    System.out.println("截取后三个字符:"+str.substring(str.length()-3,str.length())); 
   System.out.println("截取字符'f'前的字符:"+str.substring(0,str.indexOf("f")));  
   System.out.println("截取字符'f'后(包括'f')的字符:"+str.substring(str.indexOf("f"),str.length()));  
   System.out.println("截取字符'f'和字符'h'之间(包括'f')的字符:"+str.substring(str.indexOf("f"),str.indexOf("h")));  
   System.out.println("获取取第一个字符'f'的下标:"+str.indexOf("f"));  
    }
}
   发表时间:2012-03-13  
我投的新手。。
0 请登录后投票
   发表时间:2012-03-13  
灰常强大,新手来学习下了!
0 请登录后投票
   发表时间:2012-03-14  
我一般用el,s标签不用
0 请登录后投票
   发表时间:2012-03-15   最后修改:2012-03-15
这是前端版,你确定没发错?
0 请登录后投票
   发表时间:2012-03-15  
struts2的OGNL是可以直接调用后台Action中定义的方法的。
比如说
<s:property value="buildText('account.getUserName()')"/>
后台Action中定义buildText方法,然后经过处理之后,返回即可。
0 请登录后投票
   发表时间:2012-03-15  
后台的substring方法 不如利用Apache Commons工具包提供的方法
0 请登录后投票
   发表时间:2012-03-21  
前端技术。。。
0 请登录后投票
   发表时间:2012-03-21  
弱弱的问下其他喷楼的大大,EL,OGNL,jstl都属于前端的吧?除了那段System.out.println("XXOO")
0 请登录后投票
   发表时间:2012-04-10  
yjingzeming 写道
弱弱的问下其他喷楼的大大,EL,OGNL,jstl都属于前端的吧?除了那段System.out.println("XXOO")

不大。不懂那些java术语,不过,后端生成html的工作真心不算前端。前端不是后端的附庸。
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics