如何分离字符串并保存为两个变量 [英] How to separate the string and save into two variables

查看:93
本文介绍了如何分离字符串并保存为两个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何分隔字符串并保存为两个变量。但是这个字符串的值来自两个字段的组合表。现在我想把它分成两个字段

How to separate the string and save into two variables.but this value of string is coming from the table of combination of two fields.now i want to separate it into two fields

string jobHeading = Session["selectedJobHeading"].ToString();

   //in jobHeading string the value will come like this(jobposition in companyname) here jobposition and comapanyname are fields

                    string jobposting = (jobHeading.Split('in'))[0];

                    string companyname = (jobHeading.Split('in'))[1];

推荐答案

只有在满足以下条件中的一个(或两个)时才能解决问题:

You may solve the problem only if one (or both) of the following conditions hold:
  1. 组成子字符串具有固定长度。
  2. 组成子串之间有一个分隔符(即特殊字符)。
  3. 还有另一种区分子串的独特方法(例如,它们属于两个不同的集合)。





如果条件1成立,则使用 String.Substring [ ^ ]方法。



如果条件2成立则使用String.Split [ ^ ]方法。



如果条件3成立,那么你应该利用''区分方法''。



If condition 1 holds then use the String.Substring[^] method.

If condition 2 holds then use the String.Split[^] method.

If condition 3 holds then you should exploit the ''distinguishing method''.


将引号更改为双引号:

Change the quotes to double quotes:
string jobposting = (jobHeading.Split('in'))[0];

string companyname = (jobHeading.Split('in'))[1];

成为

Becomes

string jobposting = (jobHeading.Split("in"))[0];

string companyname = (jobHeading.Split("in"))[1];

但最好一次拆分,检查它,并在拆分中包含空格:

But it would be better to do the split once, and check it, and to include the spaces in the split:

string[] parts = jobHeading.Split(" in ");
if (parts.Length == 2)
   {
   string jobposting = parts[0]; 
   string companyname = parts[1];
   ...
   }


这篇关于如何分离字符串并保存为两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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