简体   繁体   English

在方法中返回新字符串

[英]Returning the new string in the method

I'm newbie to c#, So i tried the below program, which will change the case of the first character of the string 我是C#的新手,所以我尝试了以下程序,该程序将更改字符串第一个字符的大小写

public class StringBuilder
    {
        public static string ChangeFirstLetterCase(string inputdata)
        {
            if(inputdata.Length > 0)
            {
                char[] charArray = inputdata.ToCharArray();
                charArray[0] = char.IsUpper(charArray[0]) ? 
                    char.ToLower(charArray[0]) : char.ToUpper(charArray[0]);
                //return new string(charArray);
                return charArray.ToString();
            }
            return inputdata;
        }
    }
class Program
    {
        static void Main(string[] args)
        {
            var a = StringBuilder.ChangeFirstLetterCase("vishnu");
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }

Since the return type of this method ChangeFirstLetterCase() is a string. 由于此方法的返回类型ChangeFirstLetterCase()是字符串。 So I'm just doing the conversion like below 所以我只是在进行如下转换

`return charArray.ToString();`

So in the method call it is returning the System.Char[] 因此,在方法调用中,它将返回System.Char[]

Alternatively I tried the below one as well 另外,我也尝试了以下

`return new string(charArray);`

So this is returning the value as expected 因此,这将按预期返回值

Argument to the method - "vishnu"
Return value - "Vishnu"

So my question here is 所以我的问题是

  1. Since the return type of the method is string, what's wrong with below conversion? 由于该方法的返回类型为字符串,因此下面的转换有什么问题?

    return charArray.ToString();

  2. How do we know when to return as new string?. 我们如何知道何时以新字符串形式返回?

    return new string(charArray);

Please provide me some example 请提供一些例子

If you return the char array as a String, it will return you the name of the object System.Char[] . 如果将char数组作为String返回,它将返回您对象System.Char[]的名称。 This is because the ToString method of char arrays does not build the characters of the array into a usable string, but simply makes a String that states the type of object. 这是因为char数组的ToString方法不会将数组的字符构建为可用的字符串,而只是使String声明对象的类型。

However, if you use new String(char[]) , this will read the contents of the char array to build a string out of whatever characters are in the char array. 但是,如果使用new String(char[]) ,它将读取char数组的内容,以根据char数组中的任何字符构建字符串。 So, you will want to use new String(char[]) for most of your String building, I cannot think of any real uses for using the ToString() on a char array. 因此,您将需要在大多数String构建中使用new String(char[]) ,我想不出在char数组上使用ToString()的任何实际用途。

So, for your example, you should use return new String(charArray); 因此,对于您的示例,应该使用return new String(charArray); instead of return charArray.ToString(); 而不是return charArray.ToString(); .

charArray.ToString(); returns the type name because it's implemented that way, for getting string back from a character array you will always have to use String class constructor. 返回类型名称,因为它是通过这种方式实现的,要从字符数组中获取字符串,您将始终必须使用String类构造函数。

ToString method for char[] is not implemented in a way to return the character array back as a string literal, so use String constructor as you did in the second case. char[] ToString方法不能以将字符数组作为字符串文字返回的方式实现,因此,与第二种情况一样,请使用String构造函数。

You could return: 您可以返回:

if(inputData[0].IsLower())
   return string.Concat(inputData[0].ToUpper(), inputData.Substring(1));  
else 
   return string.Concat(inputData[0].ToLower(), inputData.Substring(1)); 

Your value would already be a usable string and wouldn't need to have a char[]. 您的值已经是一个可用的字符串,并且不需要使用char []。

I'm not really sure what you gain from converting the string to a char array to being with. 我不太确定从将字符串转换为char数组所获得的收益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM