?
Solved

Changing string case

Posted on 2001-07-17
36
Medium Priority
?
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 8
  • +3
36 Comments
 
LVL 44

Accepted Solution

by:
CrazyOne earned 300 total points
ID: 6291928
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
ID: 6291962
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
ID: 6291980
sorry, remove the other declaration of st under the var. i overlooked that one....
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

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

Expert Comment

by:CrazyOne
ID: 6292051
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
ID: 6292056
sorry... overlooked again. test this code in pascal. my delphi isn't working. tnx, anyway....
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6292468
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
ID: 6292737
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
ID: 6292743
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
ID: 6296648
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
ID: 6296654
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
ID: 6297864
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
ID: 6300087
 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
ID: 6300282
this answer is a summary of everyone else's solutions
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6300313
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
ID: 6300318
Oh sorry I did not see that you already rejected the proposed answer. :>)
0
 
LVL 30

Expert Comment

by:third
ID: 6301250
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
ID: 6301687
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
 
LVL 21

Expert Comment

by:gemarti
ID: 6301707
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
ID: 6301890
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
ID: 6302028
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
ID: 6302041
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
ID: 6302238
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
ID: 6302264
listening....
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6302346
>>>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
ID: 6302410
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
ID: 6302454
Hi gemarti
It is driving me crazy too. :>)
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6302476
and me too ! ;-)
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6302524
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:Mohammed Nasman
ID: 6304334
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
ID: 6313137
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
ID: 6315318
Hey Crazy One: I didn't even notice your code. Very tight.....
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6315506
Thanks gemarti. :>)
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6316309
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
ID: 6317162
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
ID: 6321161
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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