Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Truncate String by Byte
//Rextester.Program.Main is the entry point for your code. Don't change it. //Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354) using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here string chineseWord= "爱上打开链接啊可视对讲阿里卡时间段拉斯建档立卡阿加上了肯定基拉看时间的拉时间到了卡加上了肯定家属楼肯德基拉三季度拉斯建档立卡氨基酸乐凯大街阿斯利康的卢卡斯建档立卡时间的垃圾施蒂利克阿加上了肯定加上了肯定吉拉斯科技的拉丝机打了卡时间了坤道家里肯德基了凯撒奖的拉时间来得快阿家属楼肯德基爱上了空间的拉丝机离开的骄傲沙路口的加上了课建档立卡时间的拉三季度了看见爱上了肯定贾老师肯德基拉卡萨酒店卡视角乐凯大街阿斯利康的基拉看时间的可怜"; Console.WriteLine("Hello, world! Application Start"); var truncateByUTF8GetString = stringToByte(chineseWord,200); var countByte = System.Text.Encoding.UTF8.GetByteCount(truncateByUTF8GetString); Console.WriteLine("Count Byte for UTF8.GetString = " + countByte); Console.WriteLine("String truncate by UTF8.GetString= " + truncateByUTF8GetString); var truncateByLinqMethod = stringToByteLinqMethod(chineseWord,200); var countByte2 = System.Text.Encoding.UTF8.GetByteCount(truncateByLinqMethod); Console.WriteLine("Count Byte for stringToByteLinqMethod = " + countByte2); Console.WriteLine("String truncate by LinqMethod= " + truncateByLinqMethod); Console.WriteLine("Application Completed"); } static string stringToByte(string input, int maxLength){ if (input != null) { var utf8 = System.Text.Encoding.UTF8; var bytes = utf8.GetBytes(input); input = utf8.GetString(bytes, 0, bytes.Length > maxLength ? maxLength : bytes.Length); } return input; } static string stringToByteLinqMethod(string input, int maxLength){ //https://stackoverflow.com/questions/1225052/best-way-to-shorten-utf8-string-based-on-byte-length if (input != null) { input = new String(input.TakeWhile((c, i) => System.Text.Encoding.UTF8.GetByteCount(input.Substring(0, i + 1)) <= maxLength).ToArray()); } return input; } } }
run
|
edit
|
history
|
help
0
Palindrome Number
Scheduler NLog config
struc t
a
Viktor's example
project euler 18
interface
see all properties of given ldap node
Homework1
Math 5.81