Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to split and trim few characters from a string using C# for example

string = UserName: tghgd bhbj bhbh hb

Now i want to split the characters after ':' and trim those values so my final out put should be.

String = UserName

Kindly help me to do this.
Thanks in Advance.
Posted
Comments
[no name] 9-Mar-15 5:16am    
Did u mean like this :
string name = "UserName: tghgd bhbj bhbh hb";
??
and after split u want it as string name = "UserName" ?
Rocky_Bas 9-Mar-15 5:17am    
yes
gitokc 9-Mar-15 5:38am    
//--------------------------------------------
// in your function
List<string> nameList = GetNameList("UserName: tghgd bhbj bhbh hb");
foreach(string nameItem in nameList)
{
// MessageBox.Show(nameItem);
}

//--------------------------------------------

List<string> GetNameList(string name)
{
List<string> nameList = new List<string>();

string[] words = null;

int valueOf = aLine.IndexOf(":");
if (valueOf < 0) return nameList;

string nameValue = name.Substring(valueOf + 1).Trim();

words = nameValue.Split(' ');

foreach (string wordItem in words)
{
nameList.Add(wordItem);
}
return nameList;
}
//--------------------------------------------


correct ?
gitokc2015@gmail.com

Try this:
C#
string name = "UserName: tghgd bhbj bhbh hb";
name = name.Split(':').FirstOrDefault();
 
Share this answer
 
Comments
Rocky_Bas 9-Mar-15 5:34am    
Thanks praneet that worked now i have to implement such kind of conditions a lot.
[no name] 9-Mar-15 5:35am    
Glad that it worked for you :)
Try
var myStr = "string = UserName: tghgd bhbj bhbh hb";
var cln = myStr.IndexOf(":");
var output = myStr.SubString(0,cln);
 
Share this answer
 
That totally doesn't fit through, you left the important points of questions, you need to tell whether it is the first element you want, or the last or the username itself and so on.

But, let me go through a few examples, if it is the first element you want in your string, then the following code would be enough.

C#
string completeStr = "UserName: tghgd bhbj bhbh hb";

// Split and select the first element in the array string[]
// 1. Split would get a character, upon which to split the string 
// 2. We use an indexer to select an element at some index; here it is first element
string firstElement = completeStr.Split(':')[0];

// For trimming..
string trimmedStr = firstElement.Trim();


You can read the MSDN documentations to learn more about these functions, and how are they used in your application. You will find good code examples of these methods there, and some remarks from the developers about these functions.

https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx[^] Contains all of the overloaded methods for Split function.
https://msdn.microsoft.com/en-us/library/t97s7bs3%28v=vs.110%29.aspx[^] Trim function.
https://msdn.microsoft.com/en-us/library/2549tw02.aspx[^] Might be helpful to read for indexers.
 
Share this answer
 
v2
Comments
Rocky_Bas 9-Mar-15 6:17am    
Thanks for the usefull information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900