StringBuilder
How do I use String Builder
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: SupportLogger.cs
Copy
47 private void LogBasics()
48 {
49 StringBuilder sb = new StringBuilder();
50 sb.AppendFormat("SupportLogger Info: PUN {0}: ", PhotonNetwork.versionPUN);
51
52 sb.AppendFormat("AppID: {0}*** GameVersion: {1} ", PhotonNetwork.networkingPeer.mAppId.Substring(0, 8), PhotonNetwork.networkingPeer.mAppVersionPun);
53 sb.AppendFormat("Server: {0}. Region: {1} ", PhotonNetwork.ServerAddress, PhotonNetwork.networkingPeer.CloudRegion);
54 sb.AppendFormat("HostType: {0} ", PhotonNetwork.PhotonServerSettings.HostType);
55
56
57 Debug.Log(sb.ToString());
58 }
File name: ImportTiled2Unity.Material.cs
Copy
19 public Material FixMaterialForMeshRenderer(string objName, Renderer renderer)
20 {
21 string xmlPath = ImportUtils.GetXmlPath(objName);
22
23 XDocument xml = XDocument.Load(xmlPath);
24
25 // The mesh to match
26 string meshName = renderer.name;
27
28 // The mesh name may be decorated by Unity
29 string pattern = @"_MeshPart[\d]$";
30 Regex regex = new Regex(pattern);
31 meshName = regex.Replace(meshName, "");
32
33 var assignMaterials = xml.Root.Elements("AssignMaterial");
34
35 // Find an assignment that matches the mesh renderer
36 XElement match = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName);
37
38 if (match == null)
39 {
40 // The names of our meshes in the AssignMaterials elements may be wrong
41 // This happened before when Unity replaced whitespace with underscore in our named meshes
42 // That case is handled now, but there may be others
43 StringBuilder builder = new StringBuilder();
44 builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name);
45 string choices = String.Join("\n ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray());
46 builder.AppendFormat("Choices are:\n {0}", choices);
47
48 Debug.LogError(builder.ToString());
49 return null;
50 }
51
52 string materialName = match.Attribute("material").Value;
53 string materialPath = ImportUtils.GetMaterialPath(materialName);
54
55 // Assign the material
56 renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
57
58 // Set the sorting layer for the mesh
59 string sortingLayer = match.Attribute("sortingLayerName").Value;
60 if (!String.IsNullOrEmpty(sortingLayer) && !SortingLayerExposedEditor.GetSortingLayerNames().Contains(sortingLayer))
61 {
62 Debug.LogError(string.Format("Sorting Layer \"{0}\" does not exist. Check your Project Settings -> Tags and Layers", sortingLayer));
63 renderer.sortingLayerName = "Default";
64 }
65 else
66 {
67 renderer.sortingLayerName = sortingLayer;
68 }
69
70 // Set the sorting order
71 renderer.sortingOrder = ImportUtils.GetAttributeAsInt(match, "sortingOrder");
72
73 // Do we have an alpha color key?
74 string htmlColor = ImportUtils.GetAttributeAsString(match, "alphaColorKey", "");
75 if (!String.IsNullOrEmpty(htmlColor))
76 {
77 // Take for granted color is in the form '#RRGGBB'
78 byte r = byte.Parse(htmlColor.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
79 byte g = byte.Parse(htmlColor.Substring(3, 2), System.Globalization.NumberStyles.HexNumber);
80 byte b = byte.Parse(htmlColor.Substring(5, 2), System.Globalization.NumberStyles.HexNumber);
81 Color color = new Color32(r, g, b, 255);
82 renderer.sharedMaterial.SetColor("_AlphaColorKey", color);
83 }
84
85 return renderer.sharedMaterial;
86 }
File name: frmCustomers.cs
Copy
47 public static string GetUniqueKey(int maxSize)
48 {
49 char[] chars = new char[62];
50 chars = "123456789".ToCharArray();
51 byte[] data = new byte[1];
52 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
53 crypto.GetNonZeroBytes(data);
54 data = new byte[maxSize];
55 crypto.GetNonZeroBytes(data);
56 StringBuilder result = new StringBuilder(maxSize);
57 foreach (byte b in data)
58 {
59 result.Append(chars[b % (chars.Length)]);
60 }
61 return result.ToString();
62 }
File name: frmOrder.cs
Copy
31 public static string GetUniqueKey(int maxSize)
32 {
33 char[] chars = new char[62];
34 chars = "123456789".ToCharArray();
35 byte[] data = new byte[1];
36 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
37 crypto.GetNonZeroBytes(data);
38 data = new byte[maxSize];
39 crypto.GetNonZeroBytes(data);
40 StringBuilder result = new StringBuilder(maxSize);
41 foreach (byte b in data)
42 {
43 result.Append(chars[b % (chars.Length)]);
44 }
45 return result.ToString();
46 }
File name: frmPlaceOrders.cs
Copy
31 public static string GetUniqueKey(int maxSize)
32 {
33 char[] chars = new char[62];
34 chars = "123456789".ToCharArray();
35 byte[] data = new byte[1];
36 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
37 crypto.GetNonZeroBytes(data);
38 data = new byte[maxSize];
39 crypto.GetNonZeroBytes(data);
40 StringBuilder result = new StringBuilder(maxSize);
41 foreach (byte b in data)
42 {
43 result.Append(chars[b % (chars.Length)]);
44 }
45 return result.ToString();
46 }
File name: frmProduct.cs
Copy
30 public static string GetUniqueKey(int maxSize)
31 {
32 char[] chars = new char[62];
33 chars = "123456789".ToCharArray();
34 byte[] data = new byte[1];
35 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
36 crypto.GetNonZeroBytes(data);
37 data = new byte[maxSize];
38 crypto.GetNonZeroBytes(data);
39 StringBuilder result = new StringBuilder(maxSize);
40 foreach (byte b in data)
41 {
42 result.Append(chars[b % (chars.Length)]);
43 }
44 return result.ToString();
45 }
File name: frmSales.cs
Copy
31 public static string GetUniqueKey(int maxSize)
32 {
33 char[] chars = new char[62];
34 chars = "123456789".ToCharArray();
35 byte[] data = new byte[1];
36 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
37 crypto.GetNonZeroBytes(data);
38 data = new byte[maxSize];
39 crypto.GetNonZeroBytes(data);
40 StringBuilder result = new StringBuilder(maxSize);
41 foreach (byte b in data)
42 {
43 result.Append(chars[b % (chars.Length)]);
44 }
45 return result.ToString();
46 }
File name: frmStock.cs
Copy
51 public static string GetUniqueKey(int maxSize)
52 {
53 char[] chars = new char[62];
54 chars =
55 "123456789".ToCharArray();
56 byte[] data = new byte[1];
57 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
58 crypto.GetNonZeroBytes(data);
59 data = new byte[maxSize];
60 crypto.GetNonZeroBytes(data);
61 StringBuilder result = new StringBuilder(maxSize);
62 foreach (byte b in data)
63 {
64 result.Append(chars[b % (chars.Length)]);
65 }
66 return result.ToString();
67 }
File name: frmSuppliers.cs
Copy
44 public static string GetUniqueKey(int maxSize)
45 {
46 char[] chars = new char[62];
47 chars = "123456789".ToCharArray();
48 byte[] data = new byte[1];
49 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
50 crypto.GetNonZeroBytes(data);
51 data = new byte[maxSize];
52 crypto.GetNonZeroBytes(data);
53 StringBuilder result = new StringBuilder(maxSize);
54 foreach (byte b in data)
55 {
56 result.Append(chars[b % (chars.Length)]);
57 }
58 return result.ToString();
59 }
StringBuilder 138 lượt xem
Gõ tìm kiếm nhanh...