Solved

string separate in two variables asp.net c#

Posted on 2016-09-27
5
55 Views
Last Modified: 2016-09-27
We have a lot of string variables in an asp.net c# application with values like "document.doc"' "powernt,ppt", school.xls"', "theme.pdf" and son on. How can we separate in code behind in 2 new strings each variable and that 2 new variables must be: First new string is the name before the "." and the second string the extension or the name after the "."

 For example using one example I wrote:

main string : "document.doc" and this we want to store in 2 new variables like this:
first new variable value "document"
second new variable "doc"

In short is like separate the name and extension in 2 new variables.

What command or script can be used to do that in codebehind?
0
Comment
Question by:Alex E.
  • 3
5 Comments
 
LVL 42

Expert Comment

by:zephyr_hex (Megan)
ID: 41818630
You can use the Split() method.

Here is a Fiddle Demo

using System;
					
public class Program
{
	public static void Main()
	{
		var myString = "document.doc";
		var mySplit = myString.Split('.');
		for(int i = 0; i < mySplit.Length; i++) 
		{
			Console.WriteLine(mySplit[i]);
		}
	}
}

Open in new window

0
 
LVL 42

Accepted Solution

by:
zephyr_hex (Megan) earned 250 total points
ID: 41818650
Another way to do this would be to use IO.Path methods, if the string is always a file name.

Here is a Fiddle Demo

using System;
using System.IO;
					
public class Program
{
	public static void Main()
	{
		var myString = "document.doc";
		Console.WriteLine(Path.GetFileNameWithoutExtension(myString));
		Console.WriteLine(Path.GetExtension(myString).Replace(".",""));
	}
}

Open in new window

0
 
LVL 42

Expert Comment

by:zephyr_hex (Megan)
ID: 41818669
And as a side note, it may be useful to use a Class object instead of a variable

Fiddle Demo

using System;
using System.IO;
					
public class Program
{
	public static void Main()
	{
		MyThing thing = new MyThing("document.doc");
		Console.WriteLine(thing.FileName);
		Console.WriteLine(thing.Extension);
	}
	
	public class MyThing 
	{
		public string FullName { get; set; }
		public string FileName { get; set; }
		public string Extension { get; set; }
		
		public MyThing(string thing)
		{
			FullName = thing;
			FileName = Path.GetFileNameWithoutExtension(thing);
			Extension = Path.GetExtension(thing).Replace(".","");
		}
	}
}

Open in new window

0
 
LVL 12

Assisted Solution

by:Dustin Saunders
Dustin Saunders earned 250 total points
ID: 41818711
Another way is to use substring to split it up.  See this example:

            string document = "document9.27.2016.doc";
            int extensionMarker = document.LastIndexOf('.');
            string filename = document.Substring(0, extensionMarker);
            string extension = document.Substring(extensionMarker+1);

Open in new window


Be aware of the method you use though (I'd use this or IO.Path) so that if you filename contains multiple periods (as mine does above) it always looks to the last one.
0
 

Author Closing Comment

by:Alex E.
ID: 41818743
Thank you worked perfect!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now