Build Strings like a Pro

Strings are used almost everywhere. It might seem not so obvious, but string operations aren’t so great when it comes to performance.

Use the string interpolation operator ($) for smaller strings and StringBuilder for larger strings.

using System;
using System.Text;

namespace SoloLearn
{
 class Program
 {
  static void Main(string[] args)
  {
   string username = "mr_anderson";
   string email = "test@email.com";
   
   string str;
   
    // version 1
   str = "Username: " + username + ", Email: " + email;
   Console.WriteLine("version 1 output: " + str);
   
   // version 2
   str = String.Format("Username: {0}, Email: {1}", username, email);
   Console.WriteLine("version 2 output: " + str);
   
   // version 3, string interpolation
   str = $"Username: {username}, Email: {email}";
   Console.WriteLine("version 3 output: " + str);
   
   // version 4, string builder
   StringBuilder builder = new StringBuilder();
   builder.Append("Username: ");
   builder.Append(username);
   builder.Append(", Email: ");
   builder.Append(email);
   str = builder.ToString();
   Console.WriteLine("version 4 output: " + str);
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here's a possible C++ program to solve the problem: ```c++ #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream infile("SortedStrings.txt"); string prev_str, curr_str; bool sorted = true; // read the first string from the file if (getline(infile, prev_str)) { // read the rest of the strings and check if they are in ascending order while (getline(infile, curr_str)) { if (curr_str < prev_str) { sorted = false; cout << "The strings in the file are not sorted in ascending order.\n"; cout << "The first two out-of-order strings are:\n"; cout << prev_str << endl << curr_str << endl; break; } prev_str = curr_str; } } if (sorted) { cout << "The strings in the file are sorted in ascending order.\n"; } infile.close(); return 0; } ``` In this program, we first open the file "SortedStrings.txt" using an ifstream object. We then read the first string from the file using getline() function, and store it in the variable prev_str. We then start reading the rest of the strings from the file using another getline() function, and compare each string with the previous string. If we find a string that is smaller than the previous string, we set the sorted flag to false, and display the first two out-of-order strings. Otherwise, we continue reading the file until the end, and if all strings are sorted, we display a message indicating that the strings are sorted in ascending order. Note that in this program, we have used the string class to store the strings read from the file, and the < operator to compare two strings. This is possible because the string class overloads the < operator to perform lexicographic comparison of two strings.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值