String concat(String str) 学习

本文详细解析了Java中String类的concat方法实现原理,包括如何通过Arrays.copyOf和getChars方法进行字符串拼接,以及native方法System.arraycopy在拼接过程中的关键作用。

String concat(String str) 连接str到当前字符串的末尾

String s1 = “Hello”;

s1.concat(“ World”);

sout(s1);====》Hello

s1 = s1.concat(“ World”);

sout(s1);=====>Hello World

 

好奇心的引导之下看了一下concat方法的实现,源码是这样的:

    public String concat(String str) {
        int otherLen = str.length();//取得str字符串长度
        if (otherLen == 0) {//如果传入的字符串内容为空,则返回原字符串
            return this;
        }
        int len = value.length;//取得原字符串内容的长度
        char buf[] = Arrays.copyOf(value, len + otherLen);//复制字符串的内容到char数组,并扩展长度
        str.getChars(buf, len);//将str的内容复制到buf数组中,位置从len开始。在这一步完成两个字符串的拼接
        return new String(buf, true);//将buf数组转化为新的String实例并返回
    }

在刚开始看的时候会发现其中有两个比较陌生的方法:Arrays.copyOf(Char[] original,int newLength)

和 getChars(char dst[],int dstBegin)

再来看下源码:

public static char[] copyOf(char[] original, int newLength) {
        char[] copy = new char[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
void getChars(char dst[], int dstBegin) {
        System.arraycopy(value, 0, dst, dstBegin, value.length);
    }

关于上面两个方法的作用,我在第一段代码的注释中应该写的比较清楚了。

看了这两段代码之后,会发现,它们使用了同一个方法:

System.arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);

源码奉上:

public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

这里并没有方法体,那是因为该方法有一个关键字修饰:native。

了解这个关键字的朋友应该知道,这代表着该方法是原生态编辑的。也就是说实现它的方法放在了其他语言

编写的文件中。

arraycopy方法的作用是:

将src复制到dest中,src的起始位置为srcPos,dest的起始位置为destPos,复制的长度为length;

到此,学习结束

### C# 中 `string.Concat` 方法的使用说明 `string.Concat` 是 C# 提供的一个静态方法,用于将一个或多个字符串连接成单个字符串[^1]。该方法支持多种重载形式,允许用户根据需求选择适合的参数类型进行操作。 以下是 `string.Concat` 的主要功能和用法: #### 1. 基本语法 `string.Concat` 可以接受以下类型的参数: - 单个字符串数组。 - 多个字符串实例。 - 字符数组或其他可枚举的对象。 #### 2. 示例代码 以下是一些常见的使用示例: ```csharp using System; class Program { static void Main() { // 示例 1: 连接两个字符串 string str1 = "Hello "; string str2 = "World!"; string result1 = string.Concat(str1, str2); Console.WriteLine(result1); // 输出: HelloWorld! // 示例 2: 使用字符串数组 string[] strings = { "C#", " is ", "a ", "programming ", "language." }; string result2 = string.Concat(strings); Console.WriteLine(result2); // 输出: C# is a programming language. // 示例 3: 使用字符数组 char[] chars = { 'H', 'e', 'l', 'l', 'o' }; string result3 = string.Concat(chars); Console.WriteLine(result3); // 输出: Hello // 示例 4: 使用可枚举对象 List<string> list = new List<string> { "Joining", " multiple", " strings" }; string result4 = string.Concat(list); Console.WriteLine(result4); // 输出: Joining multiples trings } } ``` #### 3. 注意事项 - 如果传递给 `string.Concat` 的参数中包含 `null` 值,则会被视为空字符串处理[^1]。 - 与 `+` 操作符相比,`string.Concat` 更加高效,尤其是在处理大量字符串时。 #### 4. 性能优化 在某些情况下,如果需要频繁拼接字符串,建议使用 `StringBuilder` 类来替代 `string.Concat`,因为 `StringBuilder` 在内存管理方面表现更优。 ```csharp using System.Text; class Program { static void Main() { StringBuilder sb = new StringBuilder(); sb.Append("Using "); sb.Append("StringBuilder "); sb.Append("for "); sb.Append("efficient "); sb.Append("string "); sb.Append("concatenation."); Console.WriteLine(sb.ToString()); // 输出: Using StringBuilder for efficient string concatenation. } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值