using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
string fname, lname;
fname = "RoWan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
char[] letters = { 'H', 'e', 'l', 'l', 'o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0},{1}", greetings, 12);
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
IEnumerable<string> ienum = sarray;
string message = Join(" ", ienum);
message = String.Join(" ", ienum);
Console.WriteLine("Message: {0}", message);
DateTime waiting = new DateTime(2023, 4, 24, 10, 14, 58);
string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
Console.WriteLine("Message: {0}", chat);
Console.ReadKey();
}
public static string Join(string separator, IEnumerable<string> values)
{
if (values == null)
return string.Empty;
if (separator == null)
separator = string.Empty;
var sb = new StringBuilder();
bool isFirst = true;
foreach (var value in values)
{
if (!isFirst)
sb.Append(separator);
sb.Append(value);
isFirst = false;
}
return sb.ToString();
}
}
}
string例子总结
- char->string的方式是使用构造函数的方式就可以转换了
- {0}表示是一个占位符,具体类型可以多样化
- sting.join()函数的使用和代码实现
- stringBuilder这个类可以构建动态的字符串,方便管理,好东西
using System;
using System.Text;
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
}
public class testStructure
{
public static void Main(string[] args)
{
Books m_vBook1;
Books m_vBook2;
m_vBook1.title = "C_Programming";
m_vBook1.author = "Naha Ali";
m_vBook1.subject = "C Programming Tutorial";
m_vBook1.book_id = 6495407;
m_vBook2.title = "Telecom Billing";
m_vBook2.author = "Zara Ali";
m_vBook2.subject = "Telecom Billing Tutorial";
m_vBook2.book_id = 6495700;
Console.WriteLine("Book 1 title : {0}", m_vBook1.title);
Console.WriteLine("Book 1 author : {0}", m_vBook1.author);
Console.WriteLine("Book 1 subject : {0}", m_vBook1.subject);
Console.WriteLine("Book 1 book_id :{0}", m_vBook1.book_id);
Console.WriteLine("Book 2 title : {0}", m_vBook2.title);
Console.WriteLine("Book 2 author : {0}", m_vBook2.author);
Console.WriteLine("Book 2 subject : {0}", m_vBook2.subject);
Console.WriteLine("Book 2 book_id : {0}", m_vBook2.book_id);
Console.ReadKey();
}
}
结构体例子总结
- struct结构如果没有被new时,只有在所有字段都被初始化之后才能够使用