ASP.NET - how to remove space in string array - Asked By msakt on 19-Jun-12 02:27 AM

string array have ="aa","  bb","  cc" " dd";

i want "aa","bb","cc","dd"
S K replied to msakt on 19-Jun-12 02:31 AM
you can remove space of array element in loop use this way

string strVal = "aa,  bb,  cc,  dd"; 
for(int i =0 i<strVal.count; i++)
{
strVal[i] = strVal[i]..Replace(" ", "") ;



hope this helps you
S K replied to S K on 19-Jun-12 02:32 AM
Just correct above as

string strVal = "aa,  bb,  cc,  dd"; 
for(int i =0 i<strVal.count; i++)
{
strVal[i] = strVal[i].ToString().Replace(" ", "") ;


Jitendra Faye replied to msakt on 19-Jun-12 02:35 AM
Use this working  code--

string[] arrstrData = new string[] { "23", " ", "2"," " ,"1", "8" };
        arrstrData  = arrstrData.ToList().Where(a => a != " ").Select(a => a).ToArray() ;  

 


Output-


23
2
1
8



Try this and let me know/.
msakt replied to S K on 19-Jun-12 02:39 AM
here another issue

s ="aa"," bb","happy hour","cc"," dd","  sms send"

here happyhour is merged i want "happy hour" just front space only remove
Chintan Vaghela replied to msakt on 19-Jun-12 02:53 AM

Hi Frndz,

 

Functionality:  Remove space from  string Array

 

To achieve this task,

 

First Convert String Array to String

 

String.Join(",", strArr)

 

After then Remove white space from string

 

.Replace(" ", "")

 

After again spit this string and make string array

 

.Split(',')

 

 

Logic   :

 

  string[] strArr = { "aa", "  bb", "  cc", " dd" };

        strArr = String.Join(",", strArr).Replace(" ", "").Split(',');

 

Hope this helpful!

Thanks

 

 

 

Chintan Vaghela replied to msakt on 19-Jun-12 03:03 AM

Hi Frndz,

 

Functionality:  Remove space from  string Array

 

Using Forloop and Use Trim Function to remove space

 

Logic   :

 

 string[] strArr = { "aa", " bb", "happy hour", "cc", " dd", "  sms send" };

        string strTemp = "";

        foreach (var item in strArr)

        {

          strTemp = strTemp + "," + item.Trim();

        }

        strTemp = strTemp.TrimStart(',');

        strArr = strTemp.Split(',');Hope this helpful!

Thanks

 

 

 

TSN ... replied to msakt on 19-Jun-12 04:22 AM

Hi.. just use the trim function in order to remove the spaces in the string TRIM()

string strArr = "aa, bb, cc, dd";
for(int i =0 i<strArr.count; i++)
{
          strArr[i] = strArr[i].Trim();

}

hope this helps you