Showing posts with label word. Show all posts
Showing posts with label word. Show all posts

Saturday, March 31, 2012

trim large text

Hi! I have a function that trim large text, but the problem is that it cuts off the last word, so it's not making sense. Is there a function that trim large text in a nice way, making sure the last word is preserved. Thanks for any tip!

Can you just use Trim(myString) ?


You could also use Regular expressions :

using System.Text.RegularExpressions;

private string RemoveSpaces(string str)
{
string result = "";
Regex regulEx = new Regex(@."[\s]+");
result = regulEx.Replace(str," ");
return result;
}

Original code :Laxshmi

HTH,
Suprotim Agarwal

--
http://www.dotnetcurry.com
--



I probably didn't explain what I am doing. If I have a large text to pull from the database and I 'd like just a small portion to be displayed in gridview cell, I am uisng:

Substring(0,Math.Min(100, Eval("Bulletin").ToString().Length)) but this cuts off any last words.


Thank you for the reply! I am looking to shorten text (pulled out of the database)for display in gridview.

Cheers!


:)

Here you go. Even though this is asp code, you will get the idea

http://www.codeproject.com/asp/textpreview.asp

HTH,
Suprotim Agarwal

--
http://www.dotnetcurry.com
--


James,

if I understand you right then you're saying that you want the last word within the 100 character max limit to be preserved and to not be chopped in two, right? Here's a bit of code that might accomplish it for you if that is what you're looking for. It just involves string manipulations is all.

string myStr ="This string should be cut off at the 25th character, and preserve the final word.";string myStr25 = myStr.Substring(0, 25);int lastIdx = myStr25.LastIndexOf(" ");string finalStr = myStr25.Substring(0, lastIdx);

Let me know if you need help from there, but you should be able to adapt it to your need.


If you don't want to cut off words, just trim to the last index of a space.

string text = Eval("Bulletin").ToString();

if (text.Length > 100)

{

text = text.Substring(0, 100);

text = text.Substring(0, text.LastIndexOf(' ') + 1);

}


or how about trying this :

<ItemTemplate>
<%# Eval("Bulletin").ToString().Substring(0, 20) + "..." %>
</ItemTemplate>

Just make sure that all the strings are above 20 characters.

Edit : This code would probably cut off the last words. I do not have my visual studio with me, but what you can do is use this code with the suggestion posted by other members to get your solution.

HTH,
Suprotim Agarwal

--
http://www.dotnetcurry.com
--


I did this but it gave me 95 instead of my text:

<%# Eval("Bulletin").ToString().Substring(0,100).LastIndexOf(" ")%>

I am doing it in the aspx, makes it harder for me.


This is my datalist:

<asp:DataListID="dlBulletins"runat="server">

<ItemTemplate>

<strong><%# Eval("DisplayName")%></strong>

<br/>

<%#formatDate((DateTime)Eval("Bulletin_Date"))%><br/>

<%# Eval("Bulletin").ToString().Substring(0,Math.Min(100, Eval("Bulletin").ToString().Length)) %>

</ItemTemplate>

How can I use the string text in datalist template?

Cheers!

<separatorTemplate><hrnoshade="noshade"size="1"/>

</separatorTemplate>

</asp:DataList></p>


I am using this but it's only returning the first letter!!

<%# Eval("Bulletin").ToString().Substring(0,100).Substring(0,Eval("Bulletin").ToString().LastIndexOf(' ')) %>


It sounds like it's impossible to do this in the datalist!


Thanks friend!! I don't know what i was thinking, I made a function in the code behind called trimtext, then used it in the aspx. Work great now!

Cheers!

Trimming Text for a certian charachter or word

I have a database with unique ID numbers. Each id number has images connected to it.

For Example:

www.mysite.com/images/1/4/778814.jpg

www.mysite.com/images/"T"/"U"IDNUMBer.jpg

As you will see the "T" folder comes from the tens spont of the id number. The "U" folder come from the units spot and the image name is the ID and then I have to add jpg. Now i would never create a database like this but that is what this company is supplying me. My question is how to make asp split the id number up to use in other locations.



Thanks

RustyI can think of 2 options. See which is best for you based on the datatype of the idnumber.
a) You can work with strings and use
Dim unit, tens as String
unit = idnumber.ToString().Chars(idnumber.ToString().Length -1) 'to get units character
tens = idnumber.ToString().Chars(idnumber.ToString().Length -2) 'to get the tens character
b) Work with integers and use mathematical operators Eg:
Dim idnumber As Integer
idnumber = 456789
Dim unit, tens As Integer
unit = idnumber Mod 10 ' Returns 9
tens = (idnumber Mod 100) \ 10 ' Returns 8
Use the Split method to split the string by "/" and then you can use each of the elements of the created Array to grab the numbers.

string fullUrl = "www.mysite.com/images/1/4/778814.jpg";
string[] splitParams = fullUrl.Split('/');

splitParams[0] is "www.mysite.com"
splitParams[1] is "images"
splitParams[2] is "1"
splitParams[3] is "4"
splitParams[4] is "778814.jpg"

HTH

DJ

Thursday, March 22, 2012

Trouble with Process.start in my web application...

Hello everyone,
I have a web page which needs to open files of type .pdf (adobe) and
.doc(microsoft word).
I tried something fairly simple and have been having a lot of trouble
getting it to work.
So far just started with PDF as I figured that would be the simpler of the
two. However when that didnt seem to work.. I used a simple txt file.
Here is what I am doing
Imports System.Diagnostic
Imports System.Threading
Dim mprocess as New Process
mprocess.StartInfo.FileName = "C:\temp\readme.txt"
mprocess.Start()
mprocess.WaitForInputIdle()
Thread.Sleep(1000)
What I am expecting is to see my txt file opened up. What actually happens
is..I see notepad.exe as a process in my task manager with username ASPNET
but I do not see my file opened up...
What am I missing'
Any help and sample code in this matter would be appreciated.
Thanks in advance...
..Nishaa processes started by a service, lisk asp.net do not have access to the
desktop, so they can nott open a window. you shoudl only run commandline
programs.
-- bruce (sqlwork.com)
"Nisha" <nisha.lalvani@.rogers.com> wrote in message
news:CY2dndYOFaGolT7enZ2dnUVZ_t2dnZ2d@.ro
gers.com...
> Hello everyone,
> I have a web page which needs to open files of type .pdf (adobe) and
> .doc(microsoft word).
> I tried something fairly simple and have been having a lot of trouble
> getting it to work.
> So far just started with PDF as I figured that would be the simpler of the
> two. However when that didnt seem to work.. I used a simple txt file.
> Here is what I am doing
> Imports System.Diagnostic
> Imports System.Threading
> Dim mprocess as New Process
> mprocess.StartInfo.FileName = "C:\temp\readme.txt"
> mprocess.Start()
> mprocess.WaitForInputIdle()
> Thread.Sleep(1000)
>
> What I am expecting is to see my txt file opened up. What actually happens
> is..I see notepad.exe as a process in my task manager with username ASPNET
> but I do not see my file opened up...
> What am I missing'
> Any help and sample code in this matter would be appreciated.
>
> Thanks in advance...
> ..Nisha
>
Besides Bruce's reply, Consider this:
Even you can open a "*doc" or "*.pdf" by your server app, do you really
want to open a PDF file (by Adobe Reader) on the server and have it
WaitForInputIlde? Who is going to the server room (may be located thousands
miles away from the user, or in a locked server closet/room in your company)
to read it and close it. You propbaly meant to get a PDF/doc file and send
the end user and have it open inside a web browser, rather than opeing it on
the server.
"Nisha" <nisha.lalvani@.rogers.com> wrote in message
news:CY2dndYOFaGolT7enZ2dnUVZ_t2dnZ2d@.ro
gers.com...
> Hello everyone,
> I have a web page which needs to open files of type .pdf (adobe) and
> .doc(microsoft word).
> I tried something fairly simple and have been having a lot of trouble
> getting it to work.
> So far just started with PDF as I figured that would be the simpler of the
> two. However when that didnt seem to work.. I used a simple txt file.
> Here is what I am doing
> Imports System.Diagnostic
> Imports System.Threading
> Dim mprocess as New Process
> mprocess.StartInfo.FileName = "C:\temp\readme.txt"
> mprocess.Start()
> mprocess.WaitForInputIdle()
> Thread.Sleep(1000)
>
> What I am expecting is to see my txt file opened up. What actually happens
> is..I see notepad.exe as a process in my task manager with username ASPNET
> but I do not see my file opened up...
> What am I missing'
> Any help and sample code in this matter would be appreciated.
>
> Thanks in advance...
> ..Nisha
>