如何从C#中的其他字符串中获取字符串名称 [英] How to get string name from other string in C#

查看:95
本文介绍了如何从C#中的其他字符串中获取字符串名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个SMS软件,我想给客户选择定义他们自己的短信。我给他们一个文本框来写自己的短信,其中可以包括数据库字段名称,如亲爱的客户+ CustName +感谢您访问这里,您的账单金额是:+ BillTotal + CustNmae和BillTotal是数据库字段名称和客户添加所有其他字段名称我要把他包括在内。我想从String获取Field名称并填写其值并向客户发送短信。我的英语不是很好我希望你明白我想说的是什么..



我的尝试:



I am developing a SMS Software, i want to give Option to Customer to define their own SMS. I give them a text box to write their own SMS which can include database field names Like Dear Customer +CustName+ Thank you for visiting Here, Your Bill Amount is : +BillTotal+ CustNmae And BillTotal is Database Field name and Customer add All Other Field Name which i Show Him to Include. I want to get Field name from String and fill its value and send SMS to Customer. My English is not very well i Hope you understand what i want to say..

What I have tried:

queryacc = "SELECT Id, MessageType, Message, ModifyDate FROM PreDefineMessage where MessageType ='Sale'";
		query = "select * from customer where CustomerCode ='0001'";
		DataTable dtCust = Queries.runSQLCommandDataTable(query);
		DataTable test = runAccessQuerydt(queryacc);
		string messagepreDefine = string.Empty;
		foreach (DataRow  dr in test.Rows )
		{
			messagepreDefine = dr["Message"].ToString ();
			string ff = "Id";
			string f =  "+dr["+ff+"].ToString ()+" ;
			string oldmes = "Dear Customer Your id is : "+dr["Id"].ToString ()+"";
			oldmes = f;
			oldmes =oldmes .Insert (5,f);
			
			oldmes ="Dear customer your id is "	;
		}
		int indexp1 =messagepreDefine .IndexOf ('+');

		 messagepreDefine =messagepreDefine .Insert(indexp1+1, "dr[\"");
		 int indexp2 = messagepreDefine.IndexOf('+',indexp1+1 );
		 messagepreDefine = messagepreDefine.Insert(indexp2 + 1, "");
		foreach (DataRow dr in dtCust.Rows )
		{
			string newmessage = messagepreDefine;
		}

推荐答案

尝试用加号拆分文本



Try splitting the text by plusses

string() splits = messagepreDefine.split("+");



然后是你的第一个element splits [0]是起始文本。您的第二个是可以替换的字段名称。第三个元素也是文本等等。



例外:如果文本以+开头,则需要替换第一个拆分。测试和播放一下,你会得到它。



替代方案:用正则表达式解析 - 它会是这样的:\\ +。+ \\ \\\ +,但这是来自记忆,你也应该调整和测试。


then your first element splits[0] is the starting text. Your second is field name you can replace. Third element is again text and so on.

Exception: if the text starts with + you need to replace first split. Test and play for a bit, you'll get it.

Alternative: parse with regex - it would go something like this: \\+.+\\+, but this is from memory, you should also tweak and test.


引用:

如何从C#中的其他字符串中获取字符串名称

How to get string name from other string in C#

基本上,你没有,它不能这样工作。



做的方法你想要的是定义一些关键字,如 + CustName + + BillTotal +

然后对于每个关键字,您都有一段代码。

首先,您在文本中搜索关键字,如果找到,则替换为以编程方式获得的任意文本。

Basically, you don't, it don't work this way.

The way to do what you want is to define some keywords like +CustName+ or +BillTotal+.
Then for each keyword you have a piece of code.
First you search for the keyword in text and if found, you replace with arbitrary text that you get programmatically.


这篇关于如何从C#中的其他字符串中获取字符串名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆