MVC3.0与C#截取字符串

在MVC中,隐藏字段存放需要截取的字符串

即:

JS截取 Code
   
     
1 var getPath = "" ; // 获取分页路径
2   getPath = $( " #LeaveMsgDetail " ).find( " #hidQueryCriteriaPath " ).val();
3
4   // 隐藏字段< input type="hidden" id="hidQueryCriteriaPath" value="/BaseInfo/LeaveMessageDetail?CurrentPage=1&PageSize=3&TotalItemCount=31&p_intPageNum=0"></input>
5  
6
7 if (getPath.indexOf( " ? " ) < 0 )
8 {
9 location.herf = " @Url.Action( " LeaveMessage " , " BaseInfo " ) " ; // 返回到首页
10 }
11 else
12 {
13 var a = getPath.split( " ? " );
14 var b = a[ 1 ];
15 var c = b.split( " & " );
16 var d;
17 for (var i = 0 ; i < c.length; i ++ )
18 {
19 if (c[i].indexOf( " CurrentPage " ) >= 0 )
20 {
21 d = c[i];
22 }
23 }
24
25 var e = d.split( " = " );
26 location.href = " @Url.Action( " LeaveMessage " , " BaseInfo " ) " + " ? " + b + " &pageNum= " + e[ 1 ]; // 返回到指定页面
27 }

在C# 中,从高手那里看到的了几种方式(截取最后一位)

即:有一数组;转换为字符串后为 aaa|bbb|ccc|ddd|

 方法一 :str=str1.substring(0,lastindecof("|"));

 方法二:str=str.endTrim('1')//记得一定是 ‘ 号因为 endtrim方法的参数为char;

 方法三:str=str1.remove(str.lenght-1,1)

 方法四: str=str1.substring(0,str1.length-1)

几种方式灵活使用。

在MVC中 ,平时常见到某则新闻标题过于长,需要截取一部分,之后使用”.......“代替

<span>@StringHelper.CutStr(Model.viewListResult[i].CONTENT, 86)</span> //86代表截取43个汉字

StringHelp类 Code
   
     
1 /// <summary>
2 /// 截字符串
3 /// </summary>
4 /// <param name="sInString"> 字符串 </param>
5 /// <param name="iCutLength"> 截几个字 </param>
6 /// <returns> 截好的字符串 </returns>
7 public static string CutStr( string sInString, int iCutLength)
8 {
9 if (sInString == null || sInString.Length == 0 || iCutLength <= 0 ) return "" ;
10 int iCount = System.Text.Encoding.Default.GetByteCount(sInString);
11 if (iCount > iCutLength)
12 {
13 int iLength = 0 ;
14 for ( int i = 0 ; i < sInString.Length; i ++ )
15 {
16 int iCharLength = System.Text.Encoding.Default.GetByteCount( new char [] { sInString[i] });
17 iLength += iCharLength;
18 if (iLength == iCutLength)
19 // 需要截取的和总字符串位数相等
20 {
21 sInString = sInString.Substring( 0 , i + 1 ) ;
22 break ;
23 }
24 else if (iLength > iCutLength) // 需要截取的小于总字符长度
25 {
26 sInString = sInString.Substring( 0 , i) + " ... " ; // 。。。
27 break ;
28 }
29 }
30 }
31 return sInString;
32 }

你可能感兴趣的:(截取字符串)