String.Split与分隔符(.net)相对

时间:2009-01-18 16:45:33

标签: c# .net arrays string

有没有办法在.Net中与String.Split相反?也就是说,将数组的所有元素与给定的分隔符组合在一起。

["a", "b", "c"]并提供"a b c"" "分隔符。)

更新:我自己找到了答案。这是String.Join方法。

2 个答案:

答案 0 :(得分:123)

找到答案。它被称为String.Join

答案 1 :(得分:8)

您可以使用String.Join

public class Ball : MonoBehaviour {

    public Rigidbody2D rb;
    public Rigidbody2D hook;
    public float releaseTime = 0.15f;

    private bool isPressed = false;

    void Update()
    {
        if (isPressed)
        {

            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            if (Vector3.Distance(mousePos, hook.position) > 2.5f)
            {
                rb.position = hook.position + (mousePos - hook.position).normalized * 2.5f;
            }
            else
            {
                rb.position = mousePos;
            }
        }
    }

    void OnMouseDown()
    {
        isPressed = true;
        rb.isKinematic = true;
    }

    void OnMouseUp()
    {
        isPressed = false;
        rb.isKinematic = false;
        StartCoroutine(Release());
    }

    IEnumerator Release()
    {
        yield return new WaitForSeconds(releaseTime);
        GetComponent<SpringJoint2D>().enabled = false;
        this.enabled = false;
    }
}

虽然更详细,但您也可以使用StringBuilder方法:

string[] array = new string[] { "a", "b", "c" };
string separator = " ";
string joined = String.Join(separator, array); // "a b c"