黑莓字符串格式的相关转换

在如下五种字符格式见进行转换
"ISO-8859-1"
"UTF-8"
"UTF-16BE"
"UTF-16LE"
"US-ASCII

 

    public static String transform(String src,String srcEn,String dstEn)
    {
        String result = "";
        try
        {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(src.getBytes());          
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,srcEn);
           
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();           
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,dstEn);
           
            int ch;
            // 以字元方式显示档案内容
            while((ch = inputStreamReader.read()) != -1) {
                System.out.print((char)ch);
                outputStreamWriter.write(ch);
            }
            System.out.println();
            inputStreamReader.close();
            outputStreamWriter.close();
           
            byte rc[] = outputStream.toByteArray();
            result = new String(rc);
        }
        catch(ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
       
        return result;
    }

在C#中,可以使用字典(Dictionary)来根据字符串的前缀分类。字典是一种键值对集合,其中键是唯一的。我们可以通过检查字符串的前缀,并使用这些前缀作为字典的键,将具有相同前缀的字符串分组到一起。以下是实现这一功能的基本步骤: 1. 首先,创建一个字符串列表。 2. 然后,创建一个字典,其键类型为字符串(表示前缀),值类型为字符串列表(用于存储具有相同前缀的字符串)。 3. 遍历字符串列表,对每个字符串检查其前缀。 4. 如果字典中不存在该前缀的键,则在字典中创建一个新的字符串列表,并添加该字符串。 5. 如果字典中已存在该前缀的键,则将字符串添加到对应的列表中。 6. 最后,你可以根据需要处理字典中的分组数据。 以下是一个简单的代码示例: ```csharp using System; using System.Collections.Generic; class Program { static void Main() { // 示例字符串列表 List<string> stringList = new List<string> { "apple", "banana", "apricot", "avocado", "blackberry", "blueberry", "cherry" }; // 创建字典,键为字符串,值为字符串列表 Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>(); // 遍历字符串列表 foreach (var str in stringList) { // 获取字符串的前缀,这里假设为前三个字符 string prefix = str.Substring(0, 3); // 检查字典中是否已经有这个前缀的键 if (!dict.ContainsKey(prefix)) { // 如果没有,则创建一个新的列表并添加到字典中 dict[prefix] = new List<string>(); } // 将字符串添加到对应的列表中 dict[prefix].Add(str); } // 打印字典中的分组数据 foreach (var pair in dict) { Console.WriteLine($"前缀 {pair.Key} 的字符串有: {string.Join(", ", pair.Value)}"); } } } ``` 在这个示例中,我们创建了一个字符串列表,并使用每个字符串的前三个字符作为前缀来将字符串分类到字典中。最后,我们遍历字典并打印出每个前缀及其对应的字符串列表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值