English 中文(简体)
C#中的性权利
原标题:Right of a character in C#
  • 时间:2010-07-07 18:14:23
  •  标签:
  • c#

How do I get right(string) in C#? if user="MyDomainjKing" I want just jking from the above string.

      int index;
      string user;

    index = User.Identity.Name.IndexOf("\");
    user = (index > 0 ? User.Identity.Name.Substring(0, index) : "");
最佳回答
string user = User.Identity.Name
user= user.Remove(0, user.IndexOf(@"")+ 1);
问题回答
var user = User.Identity.Name;
var index = user.IndexOf("\");
if (index < 0 || index == user.Length - 1) 
{
    user = string.Empty;
}
else 
{
    user = user.Substring(index + 1);
}
User.Identity.Name.Split(@"")[1]

远见卓识也使它得以重新使用。 下面是一些扩展方法,其方式是仿照《XLST》的功能,substring- beforesubstring- subsequently使之具有通用性。

Usage:varuserNm = (User.Identity.Name.Contains(@”) ? 用户.Identity.Name.SubstringAfter(@):用户.Identity.Name;

public static class StringExt {
   public static string SubstringAfter(this string s, string searchString) {
      if (String.IsNullOrEmpty(searchString)) return s;
      var idx = s.IndexOf(searchString);
      return (idx < 0 ? "" : s.Substring(idx + searchString.Length));
   }

   public static string SubstringBefore(this string s, string searchString) {
      if (String.IsNullOrEmpty(searchString)) return s;
      var idx = s.IndexOf(searchString);
      return (idx < 0 ? "" : s.Substring(0, idx));
   }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签