This question already has answers here:
Remove file extension from a file name string
(13 answers)
Closed 8 years ago.
How can i get the string "tulip" from the string "tulip.jpg" using Split function in c#?
string str = "tulip.jpg";
I store the result "tulip" in str1(string type valiable).
That's a filename, so i wouldn't use String.Split but the Path-methods:
string fileNameOnly = Path.GetFileNameWithoutExtension("tulip.jpg");
For what it's worth: fileNameOnly = "tulip.jpg".Split('.')[0];
This will be a problem if the name also contains dots.
So if you insist on string methods String.Substring or String.Remove would be better:
fileNameOnly = fileName.Remove(fileName.LastIndexOf('.'));
Related
This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 12 months ago.
I have a string DisplayName which has value as "Vinny' Direct Reports". I am trying to replace "'"with "-"in string as it failing to get ingested into my Data Source.
Below is the code where I am trying to replace.
if (DisplayName.Contains("'", StringComparison.OrdinalIgnoreCase) == true)
{
DisplayName.Replace("'", "-").Replace("\"", "-");
Console.WriteLine(DisplayName);
}
Change this:
DisplayName.Replace("'", "-").Replace("\"", "-");
To this:
DisplayName = DisplayName.Replace("'", "-").Replace("\"", "-");
This question already has answers here:
How do i split a String into multiple values?
(5 answers)
Closed 3 years ago.
Not entirely sure the following code is going to help many people, but here goes
try
{
uvConnect = UniObjects.OpenSession(serverId, sUser, sPass, sAcct, "uvcs");
// Open Movie File
UniFile uvFile = uvConnect.CreateUniFile("MOVIES");
UniDynArray movieRec = uvFile.Read(txtMovieId.Text);
string sMovieData = movieRec.StringValue;
MessageBox.Show(sMovieData);
}
sMovieData contains a single string of the entire record retrieve from MOVIES file, each field is deliminated by a char(253) character in the database I am using.
Is there a function/method/etc to convert the string to an array using char(253) as a value deliminator
Something like this should work:
string[] fields = sMovieData.Split((char)253);
Try this... string[] arrayValues = "stringToConvertToArray".Split((char)253);
This question already has answers here:
How do I replace a backslash-double quote with just a double quote?
(3 answers)
Closed 5 years ago.
I am working on a c# project and have some strings like this
first string
"[\"2018\\/02\\/12\",[\"Test1\",\"Test2\",\"Test3\",\"Test4\"]]"
But this string format is not not suitable for my application. I want to change first string to this :
second string
2018-02-12,"Test1","Test2","Test3","Test4"
I've done some of it, but I'm having trouble getting a backslash. Actually backslash did not changed.
my code :
string MyString = "[\"2018\\/02\\/12\",[\"Test1\",\"Test2\",\"Test3\",\"Test4\"]]";
MyString = MyString.Replace("[", "").Replace("]", "").Replace("\\", "");
How can I get the second string?
Use the following code:
MyString.Replace("[", string.Empty).Replace("]", string.Empty).Replace("\\", string.Empty).Replace(#"\", string.Empty).Replace("/", "-");
And view the result in Text Visualizer.
This question already has answers here:
Using variables inside strings
(6 answers)
Closed 5 years ago.
string id = oUser.id.ToString();
Image1.ImageUrl="http://graph.facebook.com/893914824028397/picture?type=large&redirect=true&width=500&height=500";
I want to replace 893914824028397 by string id
You can use String Interpolation feature from C#6, so your code will look like:
string id = oUser.id.ToString();
Image1.ImageUrl=$"http://graph.facebook.com/{id}/picture?type=large&redirect=true&width=500&height=500";
This question already has answers here:
What's the # in front of a string in C#?
(9 answers)
Closed 7 years ago.
How come this string is valid to open with VLC via a Process:
string fileToPlay = #"C:\Videos\Movies\Movie title.avi";
But this one isn't:
string fileToPlay = #myMovie;
Where the value of the variable myMovie is
"C:\Videos\Movies\Movie title.avi"
Process.Start(vlcPath, fileToPlay );
The problem is that you can only use the # character when placed against string literals like this:
string path = #"c:\temp";
It can be used when placed against a string variable, as you have done, but it has a different meaning. In that case, it is used when you choose an identifier which matches a C# keyword, like this:
string #class = "hello";
You can read more about it here: https://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx