当3个逗号到来时如何将行划分为多个 [英] how to divide the row in multiple when 3 comma will come

查看:80
本文介绍了当3个逗号到来时如何将行划分为多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里当我从控制器调用地址列并查看它连续出现但我想在3个逗号之后创建下一行



喜欢地址



sar john,texas,dellas,ny,111111,

输出应该来了

sar john,texas,dellas

ny,11111



i我正在使用mvc rozer文件

here when i am calling address column from controler and view its coming in a row but i want to make next line after 3 commas

like address

sar john,texas,dellas,ny,111111,
the output should come
sar john,texas,dellas
ny,11111

i am using mvc rozer file

推荐答案

你好,



我没有进入MVC的东西。

但通常我猜你可以用你的第三个逗号的charindex替换为\ n将数据绑定到你的行时。



希望这会有所帮助。

快乐编码。
Hi,

Iam not into MVC stuff.
But normally i guess u can to a charindex for your 3rd comma and replace by \n when binding the data to your row.

Hope this helps.
Happy coding.


此方法将每个newLineEvery字段拆分给定的String(用逗号分隔的列)



This Method would split the given String every newLineEvery fields (separated columns by comma)

public string FixAddress(string address, int newLineEvery = 3){
	if(String.IsNullOrEmpty(address))
		return address;
	string separator = ",";
	string newAddress = String.Empty;
	if(address.Contains(separator)){
		int elemCount = 0;
		foreach(string elem in address.Split(separator)){
			elemCount++;
			newAddress += (elemCount % newLineEvery == 0) ? "\n"+elem : elem; 
		}
		return newAddress;
	}
	return address;
}





如果你实际上在这里使用MvcHtmlString而不是字符串字段,行为也会有所不同。 (Html Linebreaks不同,你也需要利用MvcHtmlString.Create(newAddress)作为函数的返回。



Also behavior would be different if you are actually using MvcHtmlString instead of a string field here. (Html Linebreaks are different and also you would need to utilize MvcHtmlString.Create(newAddress) as Return of the function.


你可以采取以下方法 -

1.从提供的字符串变量中找到第三个逗号的索引

You may take the following approach-
1. Find the index of 3rd comma from the supplied string variable
public int GetNthIndex(string s, char t, int n)
{
    int count = 0;
    for (int i = 0; i < s.Length; i++)
    {
        if (s[i] == t)
        {
            count++;
            if (count == n)
            {
                return i;
            }
        }
    }
    return -1;
}



参考:查找第n个出现的字符字符串



2.删除逗号&在该索引之后添加回车字符,例如


Reference: Find Nth occurrence of a character in a string

2.Remove the comma & add carriage return character after that index like

string yourString="sar john,texas,dellas,ny,111111,";
string finalString;
finalString=yourString;
int indexOfThirdIndex=0;
int counter=3;
while(indexOfThirdIndex!=(-1))
{
   int indexOfThirdIndex=GetNthIndex(finalString,',',counter);
   if(indexOfThirdIndex!=(-1))
   {
      finalString=finalString.Substring(0, indexOfThirdIndex).TrimEnd(',')+"\r\n"+inalString.Substring(indexOfThirdIndex);
   }
   counter+=3;
}





希望,它有帮助:)

N:B:我还没有在我的系统中测试了所有这些,但方法应该有效。如果没有,请告诉我。



Hope, it helps :)
N:B: I haven't tested all these in my system but the approach should work. Please let me know if it doesn't.


这篇关于当3个逗号到来时如何将行划分为多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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