Solved

Changing string case

Posted on 2001-07-17
36
158 Views
Last Modified: 2010-04-06
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
Comment
Question by:xenfung
  • 10
  • 9
  • 8
  • +3
36 Comments
 
LVL 44

Accepted Solution

by:
CrazyOne earned 75 total points
Comment Utility
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
 
LVL 30

Expert Comment

by:third
Comment Utility
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
 
LVL 30

Expert Comment

by:third
Comment Utility
sorry, remove the other declaration of st under the var. i overlooked that one....
0
 
LVL 30

Expert Comment

by:third
Comment Utility
sorry, remove the other declaration of st under the var. i overlooked that one....
0
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
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
 
LVL 30

Expert Comment

by:third
Comment Utility
sorry... overlooked again. test this code in pascal. my delphi isn't working. tnx, anyway....
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
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
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
 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
 

Author Comment

by:xenfung
Comment Utility
this answer is a summary of everyone else's solutions
0
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
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
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
Oh sorry I did not see that you already rejected the proposed answer. :>)
0
 
LVL 30

Expert Comment

by:third
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
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
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
listening....
0
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
>>>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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
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
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
Hi gemarti
It is driving me crazy too. :>)
0
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
and me too ! ;-)
0
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
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
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
 

Author Comment

by:xenfung
Comment Utility
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
 
LVL 21

Expert Comment

by:gemarti
Comment Utility
Hey Crazy One: I didn't even notice your code. Very tight.....
0
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
Thanks gemarti. :>)
0
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
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
 
LVL 44

Expert Comment

by:CrazyOne
Comment Utility
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
 
LVL 1

Expert Comment

by:h_mohsenian
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

11 Experts available now in Live!

Get 1:1 Help Now