• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Changing string case

I would like to take a html tag string like:

img name="Image1" src="MyPicture.gif" alt="A Picture!"

and convert it to:

IMG NAME="Image1" SRC="MyPicture.gif" ALT="A Picture!"

while keeping the case of the quoted substrings the same.


Can someone provide me with a function to do this?
0
xenfung
Asked:
xenfung
  • 10
  • 9
  • 8
  • +3
1 Solution
 
CrazyOneCommented:
var
  s: string = 'img name="Image1" src="MyPicture.gif" alt="A Picture!"';

procedure TForm1.Button1Click(Sender: TObject);
var
  s2, s3, sMsg: string;
  iPos: Integer;

begin

     sMsg := s + #13#10;
       repeat
              iPos := Pos('"', s);
              s2 := UpperCase(Copy(s, 1, iPos));
              s3 := s3 + s2;
              Delete(s, 1, iPos);
              iPos := Pos('"', s);
              s2 := Copy(s, 1, iPos);
              s3 := s3 + s2;
              Delete(s, 1, iPos);
       until iPos = 0;
       sMsg := sMsg + s3;
       ShowMessage(sMsg);

end;



The Crazy One
0
 
thirdCommented:
try this one....
---------------------------------------------------------
function morph(st:string):string;
var
 flag : boolean;
 i    : integer;
 st   : string;
begin
 flag := false;
 for i:= 1 to length(st) do
 begin
   if (st[i] = '"') then
     if flag = false then
       flag := true
     else
       flag := false;
   if flag = false then
      upcase(s[i]);
 end;
 morph := s;
end;
0
 
thirdCommented:
sorry, remove the other declaration of st under the var. i overlooked that one....
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
thirdCommented:
sorry, remove the other declaration of st under the var. i overlooked that one....
0
 
CrazyOneCommented:
third nice code but you forgot one thing


    if flag = false then
      s[i] := upcase(s[i]);

Rather than

    if flag = false then
      upcase(s[i]);
0
 
thirdCommented:
sorry... overlooked again. test this code in pascal. my delphi isn't working. tnx, anyway....
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

  you can do that easy by using StringReplace function, add two memo in ur form, load ur text in one memo and the other one will hold the new value, this line will change all the img to IMG in the memo1, you can add some lines also to hold the other tags

Memo2.text := StringReplace(Memo1.text, 'img', 'IMG',[rfReplaceAll])

Best regards
Mohammed Nasman
0
 
h_mohsenianCommented:
hello

it is like others but a bit smaller :

//s : ur string
// f : boolean as flag

for i :=1 to length(s) do
  if s[i] = '"' then f := not f
                else s[i] = upcase(s[i]);

best regards
hamed  
0
 
h_mohsenianCommented:
sorry:

f := true;
for i :=1 to length(s) do
 if s[i] = '"' then f := not f
               else if f then s[i] = upcase(s[i]);

sorry again
hamed

0
 
gemartiCommented:
This does the trick - small and capital letters where you want them. Capitals outside the parenthesis. Smalls inside the parenthesis.

unit cHtml;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TMemo;
    Edit2: TEdit;
    Button1: TButton;
    Edit3: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function Test : Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  y: Integer;
    result : Boolean;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i,x : integer;
  f : boolean;
  s,t,u : String;
  v : variant;

begin
  v := VarArrayCreate([0,100],varInteger);
  y := 0;
  s := Edit1.Text;
  x := length(s);
  for i := 1 to length(s) do
    begin
      if s[i] <> '"' then
        if y=0 then
            Edit2.text := Edit2.Text+upcase(s[i])
          else
            Edit2.Text := Edit2.Text+LowerCase(s[i])
       else
         Edit2.Text := Edit2.Text+UpperCase(s[i]);

            if s[i] = '"' then Form1.Test;

      end;
    end;
function TForm1.Test : Boolean;
begin
  if y = 1 then y := 0 else y := 1;
end;
end.
0
 
gemartiCommented:
Please ignore any variables I failed to remove from the code. They were there for other ideas. I got lazy and failed to remove them.

You can also ignore Edit3 and the memo field. Other ideas that didn't work :<
0
 
gemartiCommented:
I read your request again...seems my code above was almost correct...Here it is modified...

Create a new application. Add 2 edit components and 1 button.
Save the unit as CStringConvert

Paste this code in the unit.

unit CStringConvert;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
   Edit1: TEdit;
   Edit2: TEdit;
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
   procedure Test;
 public
   { Public declarations }
 end;

var
 Form1: TForm1;
 y: Integer; {y is a flag}

implementation

{$R *.dfm}
{Created By: G. E. Martin 7/19/2001
Purpose: To assist in the conversion of text strings for an HTML document.
The code below converts all text outside of quotation marks to capital letters.
All text within quotation marks remains exactly as the original text.}

procedure TForm1.Button1Click(Sender: TObject);
var

 i,x : integer;  {i is a counter variable, x is a flag}
 s : String; {String text to be evaluated and modified}

begin
 y := 0;   {Assume that you are outside of a quotation mark}
  s := Edit1.Text; {perform conversion on this text}
 for i := 1 to length(s) do
   begin
     if s[i] <> '"' then
       if y=0 then
         begin

           x := CompareStr(s[i],upcase(s[i])); {if these are not the same then
                                                conversion is necessary}
       
           if x = 0 then  {if the comparison was the same then do this..}
             Edit2.text := Edit2.Text+lowercase(s[i])
           else
             Edit2.text := Edit2.Text+upcase(s[i])
         end
       else
         begin
           x := CompareStr(s[i], upcase(s[i]));
           if x = 0 then
           Edit2.Text := Edit2.Text+upCase(s[i])
           else
           Edit2.Text := Edit2.Text+lowerCase(s[i])
         end
      else
        begin
         x := CompareStr(s[i],lowercase(s[i]));
         if x <> 0 then
        Edit2.Text := Edit2.Text+UpCase(s[i])
        else
          Edit2.Text := Edit2.Text+LowerCase(s[i]);
         end;
           if s[i] = '"' then Form1.Test; {You are either at the start (y=1) of a quotation or the
           end (y=0) of a quotation}

     end;
   end;
Procedure TForm1.Test;
begin
 if y = 1 then y := 0 else y := 1;
end;
end.
0
 
gemartiCommented:
 Delphi Programming Question  
Ask A Question   Other Questions   Reload Question    
Question: Changing string case Date: 07/17/2001 04:06PM PST  
From: xenfung
Status: Waiting for Answer  Points: 75
 Email A Friend  
I would like to take a html tag string like:

img name="Image1" src="MyPicture.gif" alt="A Picture!"

and convert it to:

IMG NAME="Image1" SRC="MyPicture.gif" ALT="A Picture!"

while keeping the case of the quoted substrings the same.


Can someone provide me with a function to do this?  
Question History
Comment
From: CrazyOne  Date: 07/17/2001 06:08PM PST  
var
 s: string = 'img name="Image1" src="MyPicture.gif" alt="A Picture!"';

procedure TForm1.Button1Click(Sender: TObject);
var
 s2, s3, sMsg: string;
 iPos: Integer;

begin

    sMsg := s + #13#10;
      repeat
             iPos := Pos('"', s);
             s2 := UpperCase(Copy(s, 1, iPos));
             s3 := s3 + s2;
             Delete(s, 1, iPos);
             iPos := Pos('"', s);
             s2 := Copy(s, 1, iPos);
             s3 := s3 + s2;
             Delete(s, 1, iPos);
      until iPos = 0;
      sMsg := sMsg + s3;
      ShowMessage(sMsg);

end;



The Crazy One  
Comment
From: third  Date: 07/17/2001 06:27PM PST  
try this one....
---------------------------------------------------------
function morph(st:string):string;
var
flag : boolean;
i    : integer;
st   : string;
begin
flag := false;
for i:= 1 to length(st) do
begin
  if (st[i] = '"') then
    if flag = false then
      flag := true
    else
      flag := false;
  if flag = false then
     upcase(s[i]);
end;
morph := s;
end;  
Comment
From: third  Date: 07/17/2001 06:41PM PST  
sorry, remove the other declaration of st under the var. i overlooked that one....
 
Comment
From: third  Date: 07/17/2001 06:47PM PST  
sorry, remove the other declaration of st under the var. i overlooked that one....
 
Comment
From: CrazyOne  Date: 07/17/2001 07:24PM PST  
third nice code but you forgot one thing


   if flag = false then
     s[i] := upcase(s[i]);

Rather than

   if flag = false then
     upcase(s[i]);  
Comment
From: third  Date: 07/17/2001 07:27PM PST  
sorry... overlooked again. test this code in pascal. my delphi isn't working. tnx, anyway....  
Comment
From: mnasman  Date: 07/17/2001 11:17PM PST  
Hello

 you can do that easy by using StringReplace function, add two memo in ur form, load ur text in one
memo and the other one will hold the new value, this line will change all the img to IMG in the memo1,
you can add some lines also to hold the other tags

Memo2.text := StringReplace(Memo1.text, 'img', 'IMG',[rfReplaceAll])

Best regards
Mohammed Nasman  
Comment
From: h_mohsenian  Date: 07/18/2001 12:43AM PST  
hello

it is like others but a bit smaller :

//s : ur string
// f : boolean as flag

for i :=1 to length(s) do
 if s[i] = '"' then f := not f
               else s[i] = upcase(s[i]);

best regards
hamed  
 
Comment
From: h_mohsenian  Date: 07/18/2001 12:45AM PST  
sorry:

f := true;
for i :=1 to length(s) do
if s[i] = '"' then f := not f
              else if f then s[i] = upcase(s[i]);

sorry again
hamed

 
Comment
From: gemarti  Date: 07/18/2001 07:24PM PST  
This does the trick - small and capital letters where you want them. Capitals outside the parenthesis.
Smalls inside the parenthesis.

unit cHtml;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
   Edit1: TEdit;
   Memo1: TMemo;
   Edit2: TEdit;
   Button1: TButton;
   Edit3: TEdit;
   Label1: TLabel;
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
   function Test : Boolean;
 public
   { Public declarations }
 end;

var
 Form1: TForm1;
 y: Integer;
   result : Boolean;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 i,x : integer;
 f : boolean;
 s,t,u : String;
 v : variant;

begin
 v := VarArrayCreate([0,100],varInteger);
 y := 0;
 s := Edit1.Text;
 x := length(s);
 for i := 1 to length(s) do
   begin
     if s[i] <> '"' then
       if y=0 then
           Edit2.text := Edit2.Text+upcase(s[i])
         else
           Edit2.Text := Edit2.Text+LowerCase(s[i])
      else
        Edit2.Text := Edit2.Text+UpperCase(s[i]);

           if s[i] = '"' then Form1.Test;

     end;
   end;
function TForm1.Test : Boolean;
begin
 if y = 1 then y := 0 else y := 1;
end;
end.
 
Comment
From: gemarti  Date: 07/18/2001 07:27PM PST  
Please ignore any variables I failed to remove from the code. They were there for other ideas. I got
lazy and failed to remove them.

You can also ignore Edit3 and the memo field. Other ideas that didn't work :<  
Comment
From: gemarti  Date: 07/19/2001 04:21AM PST  
I read your request again...seems my code above was almost correct...Here it is modified...

Create a new application. Add 2 edit components and 1 button.
Save the unit as CStringConvert

Paste this code in the unit.

unit CStringConvert;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
  Edit1: TEdit;
  Edit2: TEdit;
  Button1: TButton;
  procedure Button1Click(Sender: TObject);
private
  { Private declarations }
  procedure Test;
public
  { Public declarations }
end;

var
Form1: TForm1;
y: Integer; {y is a flag}

implementation

{$R *.dfm}
{Created By: G. E. Martin 7/19/2001
Purpose: To assist in the conversion of text strings for an HTML document.
The code below converts all text outside of quotation marks to capital letters.
All text within quotation marks remains exactly as the original text.}

procedure TForm1.Button1Click(Sender: TObject);
var

i,x : integer;  {i is a counter variable, x is a flag}
s : String; {String text to be evaluated and modified}

begin
y := 0;   {Assume that you are outside of a quotation mark}
 s := Edit1.Text; {perform conversion on this text}
for i := 1 to length(s) do
  begin
    if s[i] <> '"' then
      if y=0 then
        begin

          x := CompareStr(s[i],upcase(s[i])); {if these are not the same then
                                               conversion is necessary}
       
          if x = 0 then  {if the comparison was the same then do this..}
            Edit2.text := Edit2.Text+lowercase(s[i])
          else
            Edit2.text := Edit2.Text+upcase(s[i])
        end
      else
        begin
          x := CompareStr(s[i], upcase(s[i]));
          if x = 0 then
          Edit2.Text := Edit2.Text+upCase(s[i])
          else
          Edit2.Text := Edit2.Text+lowerCase(s[i])
        end
     else
       begin
        x := CompareStr(s[i],lowercase(s[i]));
        if x <> 0 then
       Edit2.Text := Edit2.Text+UpCase(s[i])
       else
         Edit2.Text := Edit2.Text+LowerCase(s[i]);
        end;
          if s[i] = '"' then Form1.Test; {You are either at the start (y=1) of a quotation or the
          end (y=0) of a quotation}

    end;
  end;
Procedure TForm1.Test;
begin
if y = 1 then y := 0 else y := 1;
end;
end.
 
0
 
xenfungAuthor Commented:
this answer is a summary of everyone else's solutions
0
 
CrazyOneCommented:
Yep and that aint what the proposed answer is design for. xenfung you can reject the proposed answer.

Have you tried any of these examples?
0
 
CrazyOneCommented:
Oh sorry I did not see that you already rejected the proposed answer. :>)
0
 
thirdCommented:
it seems the scenario turns to a GREED game. come on guys, where here to help each other. xenfung, any comment 'bout the answers? is there anything not cleared yet? we're waiting...
0
 
gemartiCommented:
Sorry guys. You can call me greedy, you can call me anything you want. I'm good.

The answer was posted. See the answer with the unit called CStringConvert.
0
 
gemartiCommented:
BTW - It does not seem to be very productive to make generic remarks that are obviously aimed at one person and then turn around and say we are here to help one another. Does it? I'm with the program. I don't post answers unless I am absolutely sure they work. My answer works. See the comment with the unit called CStringConvert.
0
 
h_mohsenianCommented:
dear gemarti,

take it easy;-) i think third didnt call u greedy! this q seems be a easy one but makes hard problems with us.in this site everyone wants to kill who give answer not comment;-).dont use this option at any situation.

dear xenfung & CrazyOne i think rejection of a friend suggestion that he wants just help others is not a good way.if u see again gemarti comments and answers are longer than others that means he spents his time a lot just for help.

i say it not for war just i think gemarti is right.

best regards
hamed

0
 
gemartiCommented:
Thanks h mohsenian. I did spend alot of time on this problem...

There are many times I just scan these pages for ideas and solutions. I've been a member here for a few years.
0
 
CrazyOneCommented:
h_mohsenian
When an Expert Proposes an Answer it is suppose to be somewhat unique from any other comment already made and it is also should be virtually the only correct and/or the only best solution possible. gemarti's Proposed Answer does not meet any of these criteria's. Therefore to keep the Q in the Open Question section and to help keep the Q alive the questioner should always reject the Proposed Answer when it does not meet these criteria's. That is the way EE works.

A lot of us have lobbied EE to get rid of the Proposed Answer option because it causes a lot of problems among Experts and questioners alike. The reason EE has not addressed this is issue is because they have a massive backlog of bugs to take care of and they are terribly understaffed in the Engineering department so it will be quite awhile before this issue is taken under advisement.
0
 
h_mohsenianCommented:
dear CrazyOne ,

thanks alot becouse of ur logical comments. i am a beginner in this site ( i sign up last year but never came back until 3 weaks ago that i started to use this site ) but in this few time i see 3 problems like this that Answer Option makes a problem between Ex-Ex members.i am agree with u they must rid this option.i think ur comment help me & our friend gemarti.

anyway. i can not understand what is questioner exact problem yet.that was better he say more about his q after rejection of gemarti's answer and helped him ( and us ) to understand the cause of rejectio.it was so friendlier! .i think u and our friend xenfung ( questioner ) are agree with me!

all of us are friends with a common goal. that is learn more and help each other.this is a bug ;-) if it makes problems like this among us! (although i think it is solved now ;-) ).

thanks
hamed
0
 
gemartiCommented:
listening....
0
 
CrazyOneCommented:
>>>anyway. i can not understand what is questioner exact problem yet.that was better he say more about his q after rejection of gemarti's answer and helped him ( and us ) to understand the cause of rejectio. it was so friendlier! .i think u and our friend xenfung ( questioner ) are agree with me!

Yeah I agree with you it would be nice if we could get some feed back from xenfung. Because as far as I can tell several solutions offered here will do what xenfung asked for.
0
 
gemartiCommented:
I admit, I was wrong. I thought I posted the solution to xenfung's problem because if you run the program that I posted "CStringConvert" it does exactly what he wants it to do.

I think what xenfung wants is not a procedure like I wrote, but a function that will work in any generic situation.

What is it xenfung....the suspense is driving me crazy!
0
 
CrazyOneCommented:
Hi gemarti
It is driving me crazy too. :>)
0
 
h_mohsenianCommented:
and me too ! ;-)
0
 
h_mohsenianCommented:
now if u have time please see :
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20150745

and say ur idea ( as two experts ) about my comment ( use activex ) and if u could, test it.it seems questioner forget his/her q.now it is a locked q.

thanks
hamed
0
 
Mohammed NasmanSoftware DeveloperCommented:
xenfung, have you tried my example?

StringReplace funcation doesn't  require alot of work, it's come with delphi, try it and try the other comments,then let us see what's happend with you
0
 
xenfungAuthor Commented:
I accepted CrazyOne's code because it did what I was looking for by taking the string, looking for the starting/ending quote positions, and working from there.

I found this to be better for me then simply working character by character and setting a boolean variable, as I will be working with strings that have a good amount of text in between the quotes.

Thanks to everyone for their help.


xen
0
 
gemartiCommented:
Hey Crazy One: I didn't even notice your code. Very tight.....
0
 
CrazyOneCommented:
Thanks gemarti. :>)
0
 
h_mohsenianCommented:
congratulation CrazyOne ,
but i have a q. could u pleasee List symbols meaning like :>) that u used and others. i just know :-) , ;-) and :-( .

thanks
hamed
0
 
CrazyOneCommented:
Well you listed about all I use myself. :>)

Do you know about these

LOL = Lots of Laughs
ROTFL = Rolling on the Floor Laughing
AFIAK = As Far as I Know
BTW = By the Way
0
 
h_mohsenianCommented:
thanks alot but what is the simboles that related to them ? do we use them just with chars ( LOL or :-) is correct )

thanks
hamed
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 9
  • 8
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now