【问题标题】:Write a program that outputs “I trusted you and you betrayed me” - but put it in lower camel case编写一个程序,输出“我信任你,你背叛了我”——但把它放在小写的驼峰式中
【发布时间】:2020-03-07 02:11:20
【问题描述】:

我需要编写一个输出“我信任你而你背叛了我”的程序 - 但要使用小写字母。

using System;

class IsThatAll
{
    static void Main()
    {
        string s1 = "Is ";
        string s2 = s1;
        s2 = s2 + "that ";
        //s2 = s2 + "all I ";
        string s3;
        s3 = "all ";
        s3 = s3 + "you";
        //s3 = s2;
        string s4 = "got?";
        s4 = "'ve ";
        s4 = s4 + "got?";
        Console.WriteLine(s2 + s3 + s4);
    }
}

【问题讨论】:

  • 小驼峰大写是什么意思?
  • Console.WriteLine("i trusted you and you betrayed me");
  • 你的例子与你的问题有什么关系?!

标签: c# string concatenation comments


【解决方案1】:

如果我正确理解了这个问题,你希望输出是这样的:

我信任你和你背叛了我

如果是这样,你可以这样做:

void Main()
{
    var input = "I trusted you and you betrayed me";
    var inputList = input.Split(' ').ToList();
    var output = new List<string>();
    var isFirstWord = true;
    foreach (var word in inputList)
    {
        var firstLetter = word.First();
        var restOfLetters = word.Skip(1);
        if (isFirstWord)
        {
            var newWord = $"{Char.ToLower(firstLetter)}{string.Concat(restOfLetters)}";
            output.Add(newWord);
            isFirstWord = false;
        }
        else
        {
            var newWord = $"{Char.ToUpper(firstLetter)}{string.Concat(restOfLetters)}";
            output.Add(newWord);
        }
    }
    string.Concat(output).Dump();
}

我是在 LINQPad 中完成的。

【讨论】:

    【解决方案2】:

    将字符串转换为小写或将其硬编码为小写。下面这个方法转换为小写

    String.ToLower()
    

    所以你的代码是:

     String str = "I TRUSTED YOU, YOU BETRAYED ME" 
     str = str.TwoLower();
     Console.WriteLine(str);
    

    【讨论】:

    • ToLower 只会将全文转换为小写,但他要求返回小写驼峰式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2015-12-01
    • 1970-01-01
    • 2019-12-23
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多